File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` cpp
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+
5+ int N;
6+ vector<vector<int >> v (100001);
7+ vector<int > arr;
8+ bitset<100001> vis;
9+
10+ void dfs(int n, int p) {
11+ if(n == N) vis[ n] = 1;
12+ for(int i: v [ n] ) if(i != p) {
13+ dfs(i,n);
14+ if(vis[ i] ) vis[ n] = 1;
15+ }
16+ }
17+
18+ int main() {
19+ cin.tie(0)->sync_with_stdio(0);
20+
21+ cin>>N;
22+ for(int i=1,a,b;i<N;i++) {
23+ cin>>a>>b;
24+ v[a].push_back(b);
25+ v[b].push_back(a);
26+ }
27+
28+ dfs(1,0);
29+ for(int i=1;i<=N;i++) if(!vis[i]) {
30+ int cnt = 0;
31+ queue<int> q;
32+ vis[i] = 1;
33+ q.push(i);
34+ while(!q.empty()) {
35+ int n = q.front(); q.pop();
36+ cnt++;
37+ for(int j:v[n]) if(!vis[j]) {
38+ vis[j] = 1;
39+ q.push(j);
40+ }
41+ }
42+ arr.push_back(cnt);
43+ }
44+ long long sum = 0, ans = 0;
45+ for(int i:arr) {
46+ ans += sum * i;
47+ sum += i;
48+ }
49+ cout<<ans;
50+
51+ }
52+ ```
You can’t perform that action at this time.
0 commit comments