Exemple #1
0
int main(){
    read();
    Dinic();
    for(int i=1;i<=T;i++){
        for(int j=head[i];j;j=E[j].next){
            if(E[j].a>0){
                if(E[j].f) addedge(i,E[j].v,E[j].f,0);
                E[j].f=INF;
            }
            else E[j].a=0;//去掉有负环 
        }
    }
    while(SPFA());
    printf("%d",mcost);
    return 0;
}
int main()
{
    int i,x,y;
    scanf("%d%d",&n,&m);
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(du,0,sizeof(du));
    cnt=0;
    time=1;
    top=0;
    cut=0;

    for(i=0; i<m; i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    for(int i=1; i<=n; i++) {
        if(!vis[i])
            dfs(i,0);
    }
    for(i=1; i<=n; i++) {
        for(int j=head[i]; j!=-1; j=e[j].next) {
            if(num[i]!=num[e[j].to])
                du[num[i]]++;
        }
    }
    int flag=0,p;
    for(i=1; i<=cut; i++)
        if(!du[i]) {
            flag++;
            p=i;
        }
    if(flag==1) {
        int sum=0;
        for(i=1; i<=n; i++)
            if(num[i]==p)
                sum++;
        printf("%d\n",sum);
    }
    else
        printf("0\n");
    return 0;
}
int main()
{
    int n,m,ans;
    int i,u,v;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        
        init(n);
        for(i=0;i<m;i++)
            addedge();
        ans=0;
        for(i=1;i<=n;i++)
            if(set[i] == i && 1 != num[i])
                ans+=max(1,odd[i]/2);
        printf("%d\n",ans);
    }
    return 0;
}
Exemple #4
0
/*  
    Copyright (C) 2014 Jason Giancono ([email protected])

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/
LinkedList* readNodeFile(char* filename)
{
        LinkedList* list = createList();
        FILE* file = fopen(filename, "r");
        char a[10]; 
		char b[10];
        double weight;
		//read the node info file
        while (!feof(file))
        {
            if (fscanf(file,"%s %s %lf\n",a,b,&weight))
            {
                addedge(a,b,weight,list);     
            }
        }
		fclose(file);
		return list;
}
Exemple #5
0
int main()
{
	int n,i,val,from1,from2;
	struct list *temp;
	printf("Enter number of edge in graph:: ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		printf("Enter two node name space seprated::");
		scanf("%d%d",&from1,&from2);
		printf("Enter distance between two node::");
		scanf("%d",&val);
		addinlist(from1);
		addinlist(from2);
		addedge(from1,from2,val);
	}
	for(i=1;i<=100;i++)
	{
		status[i]=0;
		pathlength[i]=9999999;
	}
	dijkstra();
	while(1)
	{
		printf("Enter distination vertex for shortest path or -1 to exit:: ");
		scanf("%d",&i);
		if(i==-1)
			break;
		else
		{
			while(1)
			{
				printf("%d",i);
				i=predecessor[i];
				if(i==-1)
					break;
				printf("<--");
			}
		}
		printf("\n");
	}
	return 0;
}
Exemple #6
0
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&l[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&r[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(sym(l[i],r[j]))
				addedge(i,j);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof(vis));
		if(dfs(i)==1) ans++;
	}
	printf("%d",ans);
	return 0;
}
Exemple #7
0
int main(int argc, char *argv[])
{
    setIO("sample");
    n = gi;int m=gi;
    for(int i = 1,u,v,w;i<=m;++i)u=gi,v=gi,w=gi,addedge(u,v,w);
    dj(1,ds,ss,qs);
    dj(n,dt,st,qt);
    for(int i = 1;i<n;++i)
     for(int j = hd[i];j;j=e[j].ne){
     int k = e[j].v;
      if(dt[k]+e[j].w == dt[i]){
       fa[i] = k,e[j].fl = true;
       addedge2(k,i);break;
      }
     }
    static int qu[MN],bot;
    qu[bot=1]=n;
    for(int i = 1;i<=bot;++i){
     int x = qu[i];
     for(int j = hd2[x];j;j=e2[j].ne) qu[++bot]=e2[j].v;
    }
    for(int i = 1;i<=bot;++i){
     int x = qu[i];
     if(ds[x]+dt[x] == ds[n]) prt[x] = x;
     else prt[x] = prt[fa[x]];
    }
    int ans = INF;
    for(int i = 1;i<=tot;++i){
     if(e[i].fl || ds[e[i].u]+e[i].w+dt[e[i].v]==ds[n])continue;
     int u = prt[e[i].u],v = prt[e[i].v];
     if(!u || !v || u==v || ds[u] > ds[v]) continue;
     int tmp = ds[e[i].u]+e[i].w+dt[e[i].v];
     ans = min(ans,tmp);
    }
    if(ans == INF )puts("-1");
    else printf("%d\n",ans);
    closeIO();
    return EXIT_SUCCESS;
}
Exemple #8
0
int main() {
	freopen("J.in", "r", stdin);
	while (scanf("%d%d", &n, &m) == 2) {
		tot = wsum = cnt = 0;
		std::fill(h + 1, h + n + 1, 0);
		std::fill(bit[0] + 1, bit[0] + n + 1, 0);
		std::fill(bit[1] + 1, bit[1] + n + 1, 0);
		for (int i = 1; i <= n; i++) {
			scanf("%d", w + i);
			wsum = (wsum + w[i]) % MOD;
		}
		for (int i = 1; i < n; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			addedge(x, y);
		}
		predfs(1);
		getanc(1, 1);
		for (int i = 1; i <= n; i++) modify(i, w[i]);
		for (int i = 1; i <= m; i++) {
			int op, x, y;
			scanf("%d%d%d", &op, &x, &y);
			if (op == 1) {
				int delta = (y - w[x]) % MOD;
				wsum = (wsum + delta) % MOD;
				modify(x, delta);
				w[x] = y;
			}
			else{
				int answer = query(x, y);
				answer = (1ll * wsum * wsum % MOD - answer) % MOD;
				answer = (answer % MOD + MOD) % MOD;
				printf("%d\n", answer);
			}
		}
	}
	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    scanf("%d",&n);
    {
        memset(fe,-1,sizeof(fe));
        memset(fq,-1,sizeof(fq));
        memset(fnq,-1,sizeof(fnq));
        memset(vis,0,sizeof(vis));
        cnte=cntq=cntnq=0;
        for(int i=1;i<=n;i++)
        {
            int tmp;scanf("%d",&tmp);
            mx[i]=mn[i]=tmp;
            up[i]=dw[i]=0;
        }
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            addedge(x,y);
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            addq(x,y,i);
        }
        lca(1,-1);
        for(int i=0;i<q;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}
int main(){
	int Ans=0;
	scanf("%d",&n);
	addedge(t,s);
	addedge(s,V+1);
	for(int i=1;i<=n;i++){
		addedge(&V[i],t);
		int k; scanf("%d",&k);
		while(k--){
			int j,w; scanf("%d%d",&j,&w);
			Ans+=w;
			delta[i][0]++;
			delta[j][1]++;
			addedge(&V[i],&V[j],inf,w);
		}
	}
	for(int i=1;i<=n;i++){
		addedge(S,&V[i],delta[i][1]);
		addedge(&V[i],T,delta[i][0]);
	}
	for(;;){
		std::queue<node*> q; node *u,*v; edge *e;
		for(int i=1;i<=n;++i) V[i].dist=inf;
		s->dist=t->dist=inf;
		S->dist=0;S->fro=NULL; T->dist=inf;
		for(q.push(S),S->inq=true;!q.empty();q.pop(),u->inq=false)
			for(e=(u=q.front())->son;e;e=e->next)
				if(e->cap&&mini((v=e->ed)->dist,u->dist+e->cost))
					{ v->fro=e; if(!v->inq&&v->dist<T->dist) q.push(v),v->inq=true; }
		if(T->dist==inf) break;
		int c=inf;
		for(e=T->fro;e;e=e->pre->fro) mini(c,e->cap);
		Ans+=T->dist*c;
		for(e=T->fro;e;e=e->pre->fro)
			e->cap-=c,E[(e-E)^1].cap+=c;
	}
	printf("%d\n",Ans);
}
Exemple #11
0
static
void
buildgraph(void)
{
    profilenode *n, *p;
    vertex *u, *v;
    edge *e;
    graphedge *g;
    profiledata d;

    for (n = (profilenode *) __mp_minimum(proftree.root); n != NULL;
         n = (profilenode *) __mp_successor(&n->node))
        if ((n->data != 0) && !(n->flags & 1))
        {
            cleardata(&d);
            sumdata(&d, &data[n->data - 1]);
            p = (profilenode *) __mp_successor(&n->node);
            while ((p != NULL) && ((p->addr == n->addr) || (!useaddresses &&
                     (p->symbol != 0) && (p->symbol == n->symbol))))
            {
                if ((p->data != 0) && !(p->flags & 1) && comparestack(n, p, 1))
                {
                    sumdata(&d, &data[p->data - 1]);
                    p->flags |= 1;
                }
                p = (profilenode *) __mp_successor(&p->node);
            }
            p = n;
            if (useaddresses || (p->symbol == 0))
                u = (vertex *) __mp_search(temptree.root,
                                           (unsigned long) p->addr);
            else
                u = (vertex *) __mp_search(temptree.root, p->symbol);
            if (u == NULL)
                u = addvertex(p);
            if ((g = __mp_findedge(&graph, &u->gnode, &graph.end)) == NULL)
                e = addedge(&u->gnode, &graph.end);
            else
                e = (edge *) ((char *) g - offsetof(edge, gnode));
            sumdata(&e->data, &d);
            u->pnode->flags |= 2;
            for (v = u; p->parent != 0; u = v)
            {
                p = &nodes[p->parent - 1];
                if (useaddresses || (p->symbol == 0))
                    v = (vertex *) __mp_search(temptree.root,
                                               (unsigned long) p->addr);
                else
                    v = (vertex *) __mp_search(temptree.root, p->symbol);
                if (v == NULL)
                    v = addvertex(p);
                if ((g = __mp_findedge(&graph, &v->gnode, &u->gnode)) == NULL)
                    e = addedge(&v->gnode, &u->gnode);
                else
                    e = (edge *) ((char *) g - offsetof(edge, gnode));
                /* Find out if we've been here before.  If we have then we have
                 * detected a cycle in the call graph and we should not
                 * contribute anything to the current edge since we have done so
                 * already.  However, we mark the edge as being part of a cycle
                 * since it is useful to know this later on.
                 */
                if (v->pnode->flags & 2)
                    e->flags |= 2;
                else
                {
                    sumdata(&e->data, &d);
                    v->pnode->flags |= 2;
                }
            }
            if ((g = __mp_findedge(&graph, &graph.start, &v->gnode)) == NULL)
                e = addedge(&graph.start, &v->gnode);
            else
                e = (edge *) ((char *) g - offsetof(edge, gnode));
            sumdata(&e->data, &d);
            /* Clear all of the flags from the current call stack that we used
             * to determine cycles in the call graph.
             */
            p = n;
            do
            {
                if (useaddresses || (p->symbol == 0))
                    v = (vertex *) __mp_search(temptree.root,
                                               (unsigned long) p->addr);
                else
                    v = (vertex *) __mp_search(temptree.root, p->symbol);
                v->pnode->flags &= ~2;
                if (p->parent != 0)
                    p = &nodes[p->parent - 1];
                else
                    p = NULL;
            }
            while (p != NULL);
        }
    for (n = (profilenode *) __mp_minimum(proftree.root); n != NULL;
         n = (profilenode *) __mp_successor(&n->node))
        n->flags = 0;
}
void Add_Edge(int u,int v,int cap,int cost)
{
	addedge(u,v,cap,cost);
	addedge(v,u,0,-cost);
}
int main() {
	freopen("C.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int tot; scanf("%d", &tot);
		for (int j = 1; j <= tot; j++) {
			int x; scanf("%d", &x);
			if (x > 0) {
				edge[x].first = i;
				pos[x].push_back(i);
			}
			else{
				edge[-x].second = i;
				neg[-x].push_back(i);
			}
		}
	}
	for (int i = 1; i <= m; i++) {
		if (!edge[i].first || !edge[i].second) continue;
		addedge(edge[i].first, edge[i].second, i);
		deg[edge[i].first]++;
		deg[edge[i].second]++;
	}
	int left = 0, right = 0;
	for (int i = 1; i <= m; i++) {
		if (edge[i].first && !edge[i].second) {
			c[i] = edge[i].first;
			for (int j = 0; j < (int)pos[i].size(); j++) {
				int now = pos[i][j];
				q[++right] = now;
				v[now] = true;
			}
		}
		else if (!edge[i].first && edge[i].second) {
			c[i] = edge[i].second;
			for (int j = 0; j < (int)neg[i].size(); j++) {
				int now = neg[i][j];
				q[++right] = now;
				v[now] = true;
			}
		}
		else c[i] = edge[i].first;
	}
	while (left < right) {
		left++;
		for (int i = h[q[left]]; i; i = e[i].next) {
			if (v[e[i].node]) continue;
			v[q[++right] = e[i].node] = true;
			c[e[i].pos] = e[i].node;
		}
	}
	left = right = 0;
	for (int i = 1; i <= n; i++) {
		if (!v[i] && deg[i] <= 1) {
			q[++right] = i;
			v[i] = true;
		}
	}
	while (left < right) {
		left++;
		for (int i = h[q[left]]; i; i = e[i].next) {
			if (v[e[i].node]) continue;
			deg[e[i].node]--;
			c[e[i].pos] = q[left];
			if (deg[e[i].node] <= 1) {
				q[++right] = e[i].node;
				v[e[i].node] = true;
			}
		}
	}
	std::fill(deg + 1, deg + n + 1, 0);
	for (int i = 1; i <= m; i++) {
		if (!edge[i].first || !edge[i].second) continue;
		if (v[edge[i].first] || v[edge[i].second]) continue;
		c[i] = edge[i].second;
		getCir(edge[i].second);
	}
	std::fill(deg + 1, deg + n + 1, 0);
	for (int i = 1; i <= m; i++) {
		if (c[i] == edge[i].first) {
			for (int j = 0; j < (int)pos[i].size(); j++) {
				deg[pos[i][j]]++;
			}
		}
		else{
			for (int j = 0; j < (int)neg[i].size(); j++) {
				deg[neg[i][j]]++;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		if (!deg[i]) {
			puts("NO");
			return 0;
		}
	}
	puts("YES");
	for (int i = 1; i <= m; i++)
		putchar('0' + (c[i] == edge[i].first));
	puts("");
	return 0;
}
Exemple #14
0
void add(int s, int t) {
	addedge(GA, s, t), addedge(GD, t, s);
}
Exemple #15
0
void solve() {
	char s[MAXSTR],t[MAXSTR];
	ll mask1=0,mask2=0,mask3;
	int i,j,tn;
	int source,sink,letters,words;
	int match[MAXSTR];
	fgets(s,MAXSTR-1,stdin); trimcrlf(s);
	for(i=j=0;;) {
		while(s[j]==' ') j++;
		if(!s[j]) break;
		t[i++]=s[j++];
	}
	t[i]=0;
	mask1=(1LL<<getix(t[0]))|(1LL<<getix(t[1]));
	for(i=2;t[i];i++) {
		if(!isalpha(t[i])) printf("illegal char [%c]\n",t[i]),exit(0);
		mask2|=(1LL<<getix(t[i]));
	}
	tn=strlen(t);
	for(ni=i=0;i<nw;i++) if((wmask[i]&mask1)==mask1 && (wmask[i]&mask2)) include[ni++]=i;
	if(ni<tn-2) {
		/* trivial reject - fewer eligible words than letters */
		puts("-1");
		return;
	}
	/* create graph */
	source=0;
	letters=1;
	words=letters+tn-2;
	sink=words+ni;
	n=sink+1;
	ne=0;
	if(n>MAXV) puts("increase MAXV and recompile"),exit(0);
	for(i=0;i<tn-2;i++) addedge(source,letters+i);
	for(i=0;i<tn-2;i++) for(j=0;j<ni;j++) {
		mask3=mask1|(1LL<<getix(t[i+2]));
		if((wmask[include[j]]&mask3)==mask3) addedge(letters+i,words+j);
	}
	for(i=0;i<ni;i++) addedge(words+i,sink);
	radixsort();
	inverseedges();
	memset(f,0,ne*sizeof(int));
	memset(cost,0,ne*sizeof(int));
	/* set capacities and costs */
	for(i=0;i<ne;i++) {
		if(from[i]==source) f[i]=1;
		if(to[i]==sink) f[i]=1,cost[i]=strlen(w[include[from[i]-words]]);
		if(from[i]==sink) f[i]=1,cost[i]=-strlen(w[include[to[i]-words]]);
		if(from[i]>=letters && from[i]<words && to[i]>=words && to[i]<sink) f[i]=1;
	}
	mincost(source,sink);
	for(i=0;i<ne;i++) if(from[i]==source && f[i]) {
		/* unmatched letter, reject */
		puts("-1");
		return;
	}
	printf("%d\n",tn-2);
	for(i=0;i<ne;i++) if(!f[i] && from[i]>=letters && from[i]<words && to[i]>=words && to[i]<sink)
		match[from[i]-letters]=to[i]-words;
	for(i=0;i<tn-2;i++) puts(w[include[match[i]]]);
}
Exemple #16
0
int main(){
	scanf("%d", &N);
	S = 0, T = 2 * N + 1;
	for(int i = 1; i < N; ++i){
		int x, y;
		scanf("%d%d", &x, &y);
		treeEdge[x].push_back(y);
		treeEdge[y].push_back(x);
	}
	
	getMark();
	for(int i = N; i > 1; --i){
		addedge(i, fa[i], T);
	}
	std::vector<std::pair<int, std::pair<int, int> > > rems;
	for(int i = 1; i <= N; ++i){
		int x, y;
		scanf("%d%d", &x, &y);
		if(depth[x] > depth[y]) std::swap(x, y);
		if(x == 1 || mark[x] == mark[y]){
			preQ[y].push(std::make_pair(depth[x], i));
		}
		else{
			addedge(S, N + i, 1);
			addedge(N + i, x, 1);
			addedge(N + i, y, 1);
			rems.push_back(std::make_pair(i, std::make_pair(x, y)));
		}
	}
	
	memset(vis, 0, sizeof(vis));
	if(!getPreDecide(1) || preQ[1].size()){
		printf("No\n");
		return 0;
	}
	for(int i = 1; i <= N; ++i){
		if(!decided[i]){
			addedge(i, T, 1);
		}
	}
	
	maxflow = 0;
	while(modlabel()){
		maxflow += aug(S, T);
	}
	if(maxflow != N - preDecide){
		printf("No\n");
		return 0;
	}
	for(int i = 1; i <= N; ++i){
		for(edge *ii = V[N + i]; ii; ii = ii -> nxt){
			if(ii -> u == 0 && ii -> t > 0 && ii -> t <= N){
				toDecide[ii -> t].push_back(i);
			}
		}
	}
	
	memset(vis, 0, sizeof(vis));
	getAnswer(1);
	
	printf("Yes\n");
	for(int i = 1; i <= N; ++i){
		printf("%d%c", ans[i], i == N ? '\n' : ' ');
	}
	
	return 0;
}
Exemple #17
0
void build_newgraph(int n) {
    memset(gscc, 0, sizeof (gscc));
    for (int i = 0; i < n; i++)
        for (edge *j = g[i]; j != NULL; j = j->nxt)
            if (idx[i] != idx[j->v])addedge(idx[i],idx[j->v], gscc);
}