标签:
2 2 4 4 2 4 2 4 2 2 2 2 5 0 5 4 1 4 3 9 1 2 3 3
Not Unique Impossible Unique 1 2 3 3
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 810; 4 const int INF = 0x3f3f3f3f; 5 struct arc{ 6 int to,flow,next; 7 arc(int x = 0,int y = 0,int z = -1){ 8 to = x; 9 flow = y; 10 next = z; 11 } 12 }e[maxn*maxn<<1]; 13 int head[maxn],d[maxn],cur[maxn],tot,S,T,n,m,k; 14 bool vis[maxn]; 15 void add(int u,int v,int flow){ 16 e[tot] = arc(v,flow,head[u]); 17 head[u] = tot++; 18 e[tot] = arc(u,0,head[v]); 19 head[v] = tot++; 20 } 21 bool bfs(){ 22 queue<int>q; 23 memset(d,-1,sizeof d); 24 d[S] = 1; 25 q.push(S); 26 while(!q.empty()){ 27 int u = q.front(); 28 q.pop(); 29 for(int i = head[u]; ~i; i = e[i].next){ 30 if(e[i].flow && d[e[i].to] == -1){ 31 d[e[i].to] = d[u] + 1; 32 q.push(e[i].to); 33 } 34 } 35 } 36 return d[T] > -1; 37 } 38 int dfs(int u,int low){ 39 if(u == T) return low; 40 int tmp = 0,a; 41 for(int &i = cur[u]; ~i; i = e[i].next){ 42 if(e[i].flow && d[e[i].to] == d[u] + 1&&(a=dfs(e[i].to,min(low,e[i].flow)))){ 43 e[i].flow -= a; 44 e[i^1].flow += a; 45 low -= a; 46 tmp += a; 47 if(!low) break; 48 } 49 } 50 if(!tmp) d[u] = -1; 51 return tmp; 52 } 53 int dinic(){ 54 int ret = 0; 55 while(bfs()){ 56 memcpy(cur,head,sizeof head); 57 ret += dfs(S,INF); 58 } 59 return ret; 60 } 61 bool dfs2(int u,int fa){ 62 if(vis[u]) return true; 63 vis[u] = true; 64 for(int i = head[u]; ~i; i = e[i].next) 65 if(e[i].flow && e[i].to != fa && dfs2(e[i].to,u)) return true; 66 return vis[u] = false; 67 } 68 int main(){ 69 int tmp,sum,ans[maxn]; 70 while(~scanf("%d %d %d",&n,&m,&k)){ 71 memset(head,-1,sizeof head); 72 S = n + m + 1; 73 T = S + 1; 74 sum = tot = 0; 75 for(int i = 1; i <= n; ++i){ 76 scanf("%d",&tmp); 77 add(S,i,tmp); 78 sum += tmp; 79 for(int j = 1; j <= m; ++j) 80 add(i,j+n,k); 81 } 82 for(int i = 1; i <= m; ++i){ 83 scanf("%d",&tmp); 84 add(i+n,T,tmp); 85 } 86 sum -= dinic(); 87 if(sum) puts("Impossible"); 88 else{ 89 bool flag = false; 90 memset(vis,false,sizeof vis); 91 for(int i = 1; i <= n; ++i) 92 if(flag = dfs2(i,-1)) break; 93 if(flag) puts("Not Unique"); 94 else{ 95 puts("Unique"); 96 for(int i = 1; i <= n; ++i){ 97 for(int j = head[i]; ~j; j = e[j].next) 98 if(e[j].to > i) ans[e[j].to] = e[j^1].flow; 99 for(int i = 1; i <= m; ++i) 100 printf("%d%c",ans[i+n],i==m?‘\n‘:‘ ‘); 101 } 102 } 103 } 104 } 105 return 0; 106 } 107 /* 108 4 2 2 109 2 2 5 0 110 5 4 111 */
HDU 4888 Redraw Beautiful Drawings
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4471911.html