标签:accept lis hat stack 多重 where lock bitset push
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2336 Accepted Submission(s): 695
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 410000, INF = 0x7fffffff, maxm = 410000; int n, m, s, t; int head[maxn], cur[maxn], vis[maxn], d[maxn], cnt, nex[maxm << 1], nex2[maxm << 1]; int head2[maxn], cnt2; int vis1[maxn], vis2[maxn]; struct node { int u, v, c, flag; }Node[maxm << 1], Edge[maxm << 1]; void add_(int u, int v, int c, int flag) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].c = c; Node[cnt].flag = flag; nex[cnt] = head[u]; head[u] = cnt++; } void add(int u, int v, int c) { add_(u, v, c, 1); add_(v, u, 0, 0); } void add2(int u, int v) { Edge[cnt2].u = u; Edge[cnt2].v = v; nex2[cnt2] = head2[u]; head2[u] = cnt2++; } bool bfs() { queue<int> Q; mem(d, 0); Q.push(s); d[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(!d[v] && Node[i].c > 0) { d[v] = d[u] + 1; Q.push(v); if(v == t) return 1; } } } return d[t] != 0; } int dfs(int u, int cap) { int ret = 0; if(u == t || cap == 0) return cap; for(int &i = cur[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(d[v] == d[u] + 1 && Node[i].c > 0) { int V = dfs(v, min(cap, Node[i].c)); Node[i].c -= V; Node[i ^ 1].c += V; ret += V; cap -= V; if(cap == 0) break; } } if(cap > 0) d[u] = -1; return ret; } int Dinic() { int ans = 0; while(bfs()) { memcpy(cur, head, sizeof head); ans += dfs(s, INF); } return ans; } int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt; stack<int> S; void dfs(int u) { pre[u] = low[u] = ++dfs_clock; S.push(u); for(int i = head2[u]; i != -1; i = nex2[i]) { int v = Edge[i].v; if(!pre[v]) { dfs(v); low[u] = min(low[u], low[v]); } else if(!sccno[v]) low[u] = min(low[u], pre[v]); } if(low[u] == pre[u]) { scc_cnt++; for(;;) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } int G[maxn], ans; int main() { int T; int kase = 0; rd(T); while(T--) { mem(head, -1), mem(head2, -1); mem(vis1, 0), mem(vis2, 0); cnt = cnt2 = 0; rd(n), rd(m); s = 0, t = maxn - 1; bool flag = 0; int tmp, u, v; int x = max(n, m); for(int i = 1; i <= n; i++) { add(s, i, 1); rd(tmp); for(int j = 1; j <= tmp; j++) { rd(v); add(i, x + v, 1); add2(i, x + v); } } for(int i = 1; i <= m; i++) add(x + i, t, 1); int max_cnt = Dinic(); int mx = x * 2; for(int i = 1; i <= m - max_cnt; i++) { mx++; add(s, mx, 1); for(int j = 1; j <= m; j++) add(mx, x + j, 1), add2(mx, x + j); } for(int i = 1; i <= n - max_cnt; i++) { mx++; add(mx, t, 1); for(int j = 1; j <= n; j++) add(j, mx, 1), add2(j, mx); } Dinic(); for(int i = 0; i < cnt; i++) { if(!Node[i].flag || Node[i].u == s || Node[i].v == t || Node[i].c != 0) continue; add2(Node[i].v, Node[i].u); } dfs_clock = scc_cnt = 0; mem(sccno, 0); mem(pre, 0); for(int i = 1; i <= mx; i++) if(!pre[i]) dfs(i); printf("Case #%d:\n", ++kase); for(int i = 1; i <= n; i++) { ans = 0; for(int j = head2[i]; j != -1; j = nex2[j]) { int v = Edge[j].v; if(sccno[i] == sccno[v] && v - x <= m) G[ans++] = v; } sort(G, G + ans); printf("%d", ans); for(int j = 0; j < ans; j++) { printf(" "); printf("%d", G[j] - x); } printf("\n"); } } return 0; }
Prince and Princess HDU - 4685(匹配 + 强连通)
标签:accept lis hat stack 多重 where lock bitset push
原文地址:https://www.cnblogs.com/WTSRUVF/p/10738310.html