1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍
分析:
直接网络流,看是否满足即可。
S向每个有需要的人连边,有床的向T连边,认识的人之间互相连边。
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; } const int N = 105, INF = 1e9; struct Edge { int to, nxt, cap; } e[50005]; int head[N], dis[N], cur[N], q[N], a[N]; int S, T, En; inline void add_edge(int u,int v,int w) { ++En; e[En].to = v, e[En].cap = w, e[En].nxt = head[u]; head[u] = En; ++En; e[En].to = u, e[En].cap = 0, e[En].nxt = head[v]; head[v] = En; } bool bfs() { for (int i = 0; i <= T; ++i) cur[i] = head[i], dis[i] = -1; int L = 1, R = 0; q[++R] = S; dis[S] = 0; while (L <= R) { int u = q[L ++]; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (dis[v] == -1 && e[i].cap > 0) { dis[v] = dis[u] + 1; q[++R] = v; if (v == T) return true; } } } return false; } int dfs(int u,int flow) { if (u == T) return flow; int used = 0; for (int &i = cur[u]; i; i = e[i].nxt) { int v = e[i].to; if (dis[v] == dis[u] + 1 && e[i].cap > 0) { int tmp = dfs(v, min(flow - used, e[i].cap)); if (tmp > 0) { used += tmp, e[i].cap -= tmp, e[i ^ 1].cap += tmp; if (used == flow) break; } } } if (used != flow) dis[u] = -1; return used; } int dinic() { int ans = 0; while (bfs()) ans += dfs(S, INF); return ans; } void solve() { En = 1;memset(head, 0, sizeof(head)); int n = read(), tot = 0; S = 0, T = n + n + 1; for (int i = 1; i <= n; ++i) { a[i] = read(); if (a[i]) add_edge(i + n, T, 1); } for (int i = 1; i <= n; ++i) { int x = read(); if ((a[i] && !x) || !a[i]) add_edge(S, i, 1), tot ++; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { int x = read(); if (x || i == j) add_edge(i, j + n, 1); } if (dinic() == tot) puts("^_^"); else puts("T_T"); } int main() { for (int Case = read(); Case --; solve()); return 0; }