Exemplo n.º 1
0
int main(){
  scanf("%d",&n);
  int a,b;
  for(i=1;i<n;i++){
    scanf("%d %d",&a,&b);
    adde(a,b);
    adde(b,a);
  }
  make(1,1);
  for(i=1;i<=n;i++){
    printf("%c ",char(ans[i]+'A'-1));
  }
  return 0;
}
Exemplo n.º 2
0
void init(int n)
{
	memset(he, -1, sizeof(he));
	memset(son, -1, sizeof(son));
	tot = 0;
	ecnt = 0;
	for (int i = 1; i < n; i++)
	{
		scanf("%d%d%d", &uu[i], &vv[i], &ll[i]);
		adde(uu[i], vv[i]);
	}
	dfs1(1, 0, 0);
	dfs2(1, 1);
	num[1] = 0;
	for (int i = 1; i < n; i++)
	{
		if (dep[uu[i]] < dep[vv[i]])
		{
			num[w[vv[i]]] = ll[i];
		}
		else
		{
			num[w[uu[i]]] = ll[i];
		}
	}
	build(1, n);
}
Exemplo n.º 3
0
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        ecnt=0;
        memset(he,-1,sizeof(he));
        memset(vis,0,sizeof(vis));
        memset(cnt,0,sizeof(cnt));
        for (int i=1; i<=m; i++)
        {
            int u,v;
            mytype l;
            scanf("%d%d%d",&u,&v,&l);
            adde(u,v,l);
        }
        int s,E;
        scanf("%d%d",&s,&E);
        for (int i=1; i<=n; i++)
            dis[i][0]=dis[i][1]=inf;
        dis[s][0]=0;
        cnt[s][0]=1;
        printf("%d\n",dijkstra_heap(n,m,s,E));
    }
    return 0;
}
Exemplo n.º 4
0
int main( void )
{
  int n, m;
  freopen("graph.in", "r", stdin);
  freopen("graph.out", "w", stdout);
  scanf("%i %i", &n, &m);
  int s, q, wt;
  for (int i = 0; i < n; i++)
    head[i] = -1;
  for (int i = 0; i < m; i++)
  {
    scanf("%i%i%i", &s, &q, &wt);
    adde(s - 1, q - 1, 12 / wt);
    adde(q - 1, s - 1, 12 / wt);
  }
  d[0] = 0;
  for (int i = 1; i < n; i++)
    d[i] = MaxE;
  bfs();
  for (int i = 1; i < n; i++)
    printf("%.10lf\n", ((double)d[i]) / 12);
  return 0;
}
Exemplo n.º 5
0
bool two_sat(int n,int m)
{
    ecnt=idcnt=ccnt=top=0;
    for (int i=1; i<=2*n; i++)
    {
        he[i]=-1;
        dfn[i]=instk[i]=0;
        c[i].clear();
    }
    for (int i=1; i<=n; i++)
        sol[i]=-1;
    for (int i=0; i<m; i++)
    {
        ///TODO: adde
    }
    adde(1,n+1);
    for (int i=1; i<=2*n; i++)
        if (!dfn[i])
            dfs(i);
    for (int i=1; i<=n; i++)
        if (belong[i]==belong[i+n])
            return 0;
    for (int i=1; i<=ccnt; i++)
    {
        int val=1;
        for (int j=0; j<c[i].size(); j++)
        {
            int cur=c[i][j];
            if (getvalue(n,cur)==0)
                val=0;
            for (int k=he[cur]; k!=-1; k=E[k].next)
                if (getvalue(n,E[k].v)==0)
                    val=0;
            if (val==0)
                break;
        }
        for (int j=0; j<c[i].size(); j++)
            if (c[i][j]>n)
                sol[c[i][j]-n]=!val;
            else
                sol[c[i][j]]=val;
    }
    return 1;
}
Exemplo n.º 6
0
int main() {
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; ++i) {
                scanf("%d", an + i); 
                black[--an[i]] = 1;
        }
        for(int i = 0, s, t, c; i < n-1; ++i) {
                scanf("%d%d%d", &s, &t, &c);
                --s; --t;
                adde(s, t, c); adde(t, s, c);
        }

        dis[an[0]] = 0;
        dfs(an[0], -1);
        S = an[0];
        for(int i = 0; i < m; ++i) if(dis[S] < dis[an[i]]) S = an[i];
        dis[S] = 0;
        dfs(S, -1);
        T = S;
        for(int i = 0; i < m; ++i) if(dis[T] < dis[an[i]]) T = an[i];
        dia_top = 0;
        for(int i = T; ~i; i = fa[i]) {
                special[i] = 1;
                diameter[dia_top++] = i;
        }
        dis2[T] = 0;
        stk_top = 0;
        res = 0, resCnt = 0;
        dfs2(T, -1, dis2);

        int cnt = 0;
        for(int i = 0, idx; i < dia_top; ++i) {
                idx = diameter[i];
                ans[idx] += cnt;
                snuke(it, point[idx]) {
                        if(dis2[*it] < dis[*it]) {
                                ++cnt;
                        }
                        if(dis2[*it] == dis2[S]) {
                                cnt = 0; break;
                        }
                }
        }

        cnt = 0;
        for(int i = dia_top-1, idx; i >= 0; --i) {
                idx = diameter[i];
                ans[idx] += cnt;
                snuke(it, point[idx]) {
                        if(dis2[*it] > dis[*it]) {
                                ++cnt;
                        }
                        if(dis[*it] == dis[T]) {
                                cnt = 0; break;
                        }
                }
                
        }

        for(int i = 0, idx; i < dia_top; ++i) {
                idx = diameter[i];
                if(black[idx]) continue;
                ans[idx] += point[idx].size();
                if(res < ans[idx]) res = ans[idx], resCnt = 1;
                else if(res == ans[idx]) ++resCnt;
        }
        if(!res) printf("%d %d\n", 0, n - m);
        else printf("%d %d\n", res, resCnt);
        return 0;
}
// like Edmonds-Karp Algorithm
inline void add(int a,int b,int c,int d)
{ adde(a,b,c,d);adde(b,a,0,-d);}