-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadjoin.cpp
60 lines (56 loc) · 1.07 KB
/
adjoin.cpp
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
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
typedef map<int, int> mii;
#define LSOne(S) (S & (-S))
int c,l,x,y;
bool visited[10005] = {0};
vector<vi> g;
int f(int node) {
visited[node] = true;
if(g[node].empty()) return 0;
int maxD = 0, minD = 0;
for(int i = 0; i < g[node].size(); i++) {
if(!visited[g[node][i]]) {
int nextD = f(g[node][i]) + 1;
maxD = max(maxD, nextD);
minD = min(minD, nextD);
}
}
return (maxD + minD + 1) / 2;
}
int main() {
cin >> c >> l;
for(int i = 0; i < c; i++) {
g.push_back(vi());
}
for(int i = 0; i < l; i++) {
cin >> x >> y;
g[x].push_back(y);
}
int curr = f(0);
bool allVisited = true;
int notVisited = -1;
for(int i = 0; i < c; i++) {
if(!visited[i]) {
allVisited = false;
notVisited = i;
break;
}
}
while(!allVisited) {
curr += f(notVisited);
++curr;
allVisited = true;
for(int i = 0; i < c; i++) {
if(!visited[i]) {
allVisited = false;
notVisited = i;
break;
}
}
}
cout << curr << endl;
}