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
| #include <bits/stdc++.h> #define ios ios::sync_with_stdio(0),cin.tie(0); #define int long long #define N 30001 using namespace std; int n,m,root,lv[N],low[N],tree_cnt[N],es = 1; bool visit[N],ans[N]; vector<int> edge[N];
int DFS(int now,int father){ visit[now] = 1; lv[now] = es; low[now] = es++; int len = edge[now].size(),sum=1; for(int i=0;i<len;i++){ int next = edge[now][i]; if(!visit[next]){ int temp = DFS(next,now); sum += temp; if(low[next] >= lv[now] && now!=root){ ans[now] = 1; tree_cnt[now] += temp; } } if(next!=father)low[now] = min(low[now],low[next]); } return sum; }
signed main(){ ios; cin>>n>>m; for(int i=0;i<m;i++){ int x,y;cin>>x>>y; edge[x].push_back(y); edge[y].push_back(x); } cin>>root; memset(ans, 0, sizeof(ans)); memset(visit, 0, sizeof(visit)); memset(tree_cnt, 0, sizeof(tree_cnt)); int sum = DFS(root,root),min_cnt = INT_MAX,min_pos = 0; for(int i=1;i<=n;i++){ int temp = sum-tree_cnt[i]; if(ans[i] && temp < min_cnt){ min_pos = i; min_cnt = temp; } } if(min_cnt == INT_MAX)cout<<0<<endl; else cout<<min_pos<<" "<<min_cnt<<endl; }
|