Hướng dẫn giải của Mạng điện
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <vector> #include <queue> using namespace std; int n,ans,s,t,isCut[1010],child[1010],ok[1010],vs[1010]; int low[1010],num[1010],step,d[1010],e[1010]; vector <int> a[1010]; void visit(int x) { low[x]=num[x]=++step; for (int i=0;i<int(a[x].size());i++) { int y=a[x][i]; if (y==d[x]) continue; if (num[y]) low[x]=min(low[x],num[y]); else d[y]=x, visit(y), low[x]=min(low[x],low[y]); } } void bfs(int x,int cut,int d[]) { for (int i=1;i<=n;i++) d[i]=0; queue <int> q; q.push(x); d[x]=1; d[cut]=1; if (x==cut) return; while (!q.empty()) { x=q.front(); q.pop(); for (int i=0;i<int(a[x].size());i++) { int y=a[x][i]; if (!d[y]) d[y]=1, q.push(y); } } } int connected(int x,int y) { vs[x]=1; if (x==y) return 1; for (int i=0;i<int(a[x].size());i++) if (!vs[a[x][i]] && connected(a[x][i],y)) return 1; return 0; } int main() { int m,x,y; cin >> n >> m >> s >> t; while (m--) cin >> x >> y, a[x].push_back(y), a[y].push_back(x); if (!connected(s,t)) { cout << 0 << endl; return 0; } for (int i=1;i<=n;i++) if (!d[i]) visit(i); for (int i=1;i<=n;i++) child[d[i]]++; for (int i=1;i<=n;i++) if (d[i] && !isCut[d[i]]) if (low[i]>=num[d[i]] && (d[d[i]] || child[d[i]]>1)) isCut[d[i]]=1; for (int i=1;i<=n;i++) ok[i]=1; for (int i=1;i<=n;i++) if (isCut[i]) { bfs(s,i,d); bfs(t,i,e); for (int j=1;j<=n;j++) ok[j]&=(d[j]|e[j]); } for (int i=1;i<=n;i++) ans+=ok[i]; cout << ans << endl; for (int i=1;i<=n;i++) if (ok[i]) cout << i << endl; }
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 3; int tin[N], low[N]; bool was[N]; vector<int> a[N]; set<int> answer; int n, m, s, t; void visit(int u) { was[u] = true; for (int v : a[u]) if (!was[v]) visit(v); } bool sameComponent() { visit(s); return was[t]; } stack<pair<int, int> > S; int Time; void dfs(int u, int p = 0) { tin[u] = ++Time; low[u] = N; for (int v : a[u]) if (v != p) { if (tin[v]) { low[u] = min(low[u], tin[v]); } else { S.push({u, v}); dfs(v, u); low[u] = min(low[u], low[v]); if (low[v] >= tin[u]) { set<int> cur; while (S.top() != make_pair(u, v)) { cur.insert(S.top().second); S.pop(); } cur.insert(u); cur.insert(v); S.pop(); if (cur.count(s) && cur.count(t)) { answer = cur; } } } } } int main() { ios::sync_with_stdio(false); cin >> n >> m >> s >> t; bool hasEdge = false; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; a[u].push_back(v); a[v].push_back(u); hasEdge |= (u == s && v == t) || (u == t && v == s); } if (!sameComponent()) return cout << 0, 0; if (!hasEdge) a[s].push_back(t), a[t].push_back(s); for (int i = 1; i <= n; ++i) if (!tin[i]) dfs(i); cout << answer.size() << endl; for (int u : answer) cout << u << '\n'; return 0; }
Code mẫu của RR
//Wishing myself a happy lunar new year with a lot of accept solutions //Written by Nguyen Thanh Trung {$R+,Q+} uses math; const FINP=''; FOUT=''; MAXN=2002; var f1,f2:text; n,s,t,count,first,last:longint; fl,a,c:array[0..MAXN,0..MAXN] of longint; deg,luu,ok,xet,queue,trace:array[0..MAXN] of longint; path:boolean; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure inp; var m,u,v,i:longint; begin path:=false; read(f1,n,m,s,t); for i:=1 to n do if (i<>s) and (i<>t) then begin deg[i]:=1; a[i,1]:=i+n; c[i,i+n]:=1; end else begin deg[i]:=1; a[i,1]:=0; c[i,0]:=1; end; for i:=1 to m do begin read(f1,u,v); if ((u=s) and (v=t)) or ((u=t) and (v=s)) then begin path:=true; continue; end; inc(deg[u+n]); a[u+n,deg[u+n]]:=v; c[u+n,v]:=2; inc(deg[v+n]); a[v+n,deg[v+n]]:=u; c[v+n,u]:=2; end; end; procedure ans; var i:longint; begin if count>0 then begin ok[s]:=1; ok[t]:=1; inc(count,2); end; if (count=0) and path then begin ok[s]:=1; ok[t]:=1; count:=2; end; writeln(f2,count); for i:=1 to n do if ok[i]=1 then writeln(f2,i); end; procedure init(u:longint); begin fillchar(xet,sizeof(xet),0); xet[u]:=1; fillchar(trace,sizeof(trace),0); first:=1; last:=1; queue[1]:=u; end; procedure findPath; var u,i,v:longint; begin while first<=last do begin u:=queue[first]; inc(first); for i:=1 to deg[u] do begin v:=a[u,i]; if (xet[v]=0) and (c[u,v]>fl[u,v]) then begin trace[v]:=u; xet[v]:=1; inc(last); queue[last]:=v; end else if (xet[v]=0) and (fl[v,u]>0) then begin trace[v]:=-u; xet[v]:=1; inc(last); queue[last]:=v; end; if xet[0]=1 then exit; end; end; end; procedure incFlow(start:longint); var v,u:longint; begin u:=0; while u<>start do begin v:=trace[u]; fl[v,u]:=1; u:=v; end; end; procedure erase(start:longint); var v,u:longint; begin u:=0; while u<>start do begin v:=luu[u]; fl[v,u]:=0; u:=v; end; end; procedure solve; var i:longint; begin count:=0; for i:=1 to n do if (i<>s) and (i<>t) then begin c[i,i+n]:=2; init(i); findPath; if xet[0]=0 then begin c[i,i+n]:=1; continue; end; incFlow(i); luu:=trace; init(i); findPath; if xet[0]=0 then begin c[i,i+n]:=1; erase(i); continue; end; erase(i); ok[i]:=1; inc(count); c[i,i+n]:=1; end; end; begin openF; inp; solve; ans; closeF; end.
Bình luận