BZOJ1116:[POI2008]CLO & 加强版on Luogu P3465
bzoj没spj,就并查集判一下每个联通块是否有环就好了
Luogu的有spj,这就有点麻烦
手玩一下发现对于每个联通块走一个环,剩下的从环出发走个树就好了
对于每个环记一下伸出树枝的点
调这题快调死了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cctype> #include<cstdio> #include<bitset> using namespace std; const int MAXN = 100005, MAXM = 200005; struct EDGE{ int nxt, to; EDGE() {nxt = to = 0;} }edge[MAXM << 1]; int n, m, tot, totedge, getcir; int fa[MAXN], head[MAXN], deg[MAXN], incir[MAXN], getin[MAXN]; bool has[MAXN], vis[MAXN], def[MAXM << 1]; inline int opp( int x) { return (x + ((x & 1) ? (1) : (-1))); } inline void add( int x, int y) { edge[++totedge].to = y; edge[totedge].nxt = head[x]; head[x] = totedge; return ; } int findfa( int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); } inline void link( int x, int y) { int fx = findfa(x), fy = findfa(y); if (fx == fy) { has[fx] = has[fy] = true ; return ; } fa[fx] = fy; has[fy] = has[fx] = (has[fx] | has[fy]); return ; } int dfs( int x, int from) { if (vis[x]) { getcir = x; return x; } vis[x] = true ; for ( int i = head[x]; i; i = edge[i].nxt) { int y = edge[i].to; if (y == from) continue ; incir[x] = dfs(y, x); if (incir[x] || getcir) break ; } return (x == incir[x]) ? 0 : incir[x]; } void efs( int x, int from) { for ( int i = head[x]; i; i = edge[i].nxt) if (!def[i]) { int y = edge[i].to; if (y == from) continue ; if (!getin[y]) { getin[y] = x; def[i] = def[opp(i)] = true ; efs(y, x); } } return ; } int main() { scanf ( "%d%d" , &n, &m); tot = n; for ( int i = 1; i <= n; ++i) fa[i] = i; if (m < n) { puts ( "NIE" ); return 0; } register int x, y; for ( int i = 1; i <= m; ++i) { scanf ( "%d%d" , &x, &y); add(x, y); add(y, x); link(x, y); } for ( int i = 1; i <= n; ++i) { fa[i] = findfa(i); if (!has[fa[i]]) { puts ( "NIE" ); return 0; } } puts ( "TAK" ); int lastfind = 0; for ( int i = 1; i <= n; ++i) { if (fa[i] != lastfind) { lastfind = fa[i]; getcir = 0; dfs(i, 0); } } lastfind = 0; for ( int i = 1; i <= n; ++i) { if ((incir[i] == i) && (!getin[i])) { efs(i, 0); } } for ( int i = 1; i <= n; ++i) printf ( "%d\n" , getin[i]); return 0; } |
禁止诸如开发者知识库/布布扣/码迷/学步园/马开东等 copy 他人博文乃至博客的网站转载
,用户转载请注明出处:https://www.cnblogs.com/xcysblog/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步