//https://oj.uz/problem/view/JOI14_friends
#include<bits/stdc++.h>
const int N = 1e5 + 5;
using namespace std;
vector <int> adj[N];
int n, m, pset[N], siz[N], ans;
bool check[N];
int findset(int i) {return ((pset[i] == i) ? i : pset[i] = findset(pset[i]));}
bool unionset(int i, int j){
i = findset(i); j = findset(j);
if (i == j) return false;
siz[j] += siz[i];
pset[i] = j;
return true;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
}
for (int i = 1; i <= n; i++) pset[i] = i, siz[i] = 1;
for (int u = 1; u <= n; u++){
if ((int)adj[u].size() >= 2){
int x = adj[u][0]; check[x] = true;
for (int i = 1; i < adj[u].size(); i++){
int v = adj[u][i];
check[v] = true;
unionset(x, v);
}
}
}
queue <int> mq;
for (int i = 1; i <= n; i++) if (check[i]) mq.push(i);
while (mq.size()){
int u = mq.front(); mq.pop();
for (int i = 0; i < adj[u].size(); i++){
int v = adj[u][i];
if (!check[v]) check[v] = true, mq.push(v);
unionset(u, v);
}
}
for (int i = 1; i <= n; i++) if (pset[i] == i) ans += siz[i] * (siz[i] - 1);
for (int i = 1; i <= n; i++) if (pset[i] == i && siz[i] == 1) ans += (int)adj[i].size();
cout << ans << "\n";
}
Compilation message
friends.cpp: In function 'int main()':
friends.cpp:33:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < adj[u].size(); i++){
^
friends.cpp:44:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2788 KB |
Output is correct |
3 |
Correct |
4 ms |
2896 KB |
Output is correct |
4 |
Correct |
4 ms |
2896 KB |
Output is correct |
5 |
Correct |
3 ms |
2956 KB |
Output is correct |
6 |
Correct |
3 ms |
3044 KB |
Output is correct |
7 |
Correct |
3 ms |
3044 KB |
Output is correct |
8 |
Correct |
4 ms |
3180 KB |
Output is correct |
9 |
Correct |
4 ms |
3180 KB |
Output is correct |
10 |
Correct |
4 ms |
3180 KB |
Output is correct |
11 |
Correct |
4 ms |
3180 KB |
Output is correct |
12 |
Correct |
3 ms |
3180 KB |
Output is correct |
13 |
Correct |
4 ms |
3180 KB |
Output is correct |
14 |
Correct |
4 ms |
3180 KB |
Output is correct |
15 |
Correct |
3 ms |
3180 KB |
Output is correct |
16 |
Correct |
3 ms |
3180 KB |
Output is correct |
17 |
Correct |
4 ms |
3180 KB |
Output is correct |
18 |
Correct |
4 ms |
3180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3388 KB |
Output is correct |
2 |
Correct |
4 ms |
3388 KB |
Output is correct |
3 |
Correct |
9 ms |
3748 KB |
Output is correct |
4 |
Correct |
57 ms |
5820 KB |
Output is correct |
5 |
Correct |
34 ms |
6696 KB |
Output is correct |
6 |
Correct |
55 ms |
9244 KB |
Output is correct |
7 |
Correct |
5 ms |
9244 KB |
Output is correct |
8 |
Correct |
6 ms |
9244 KB |
Output is correct |
9 |
Correct |
11 ms |
9244 KB |
Output is correct |
10 |
Correct |
17 ms |
9244 KB |
Output is correct |
11 |
Correct |
50 ms |
11848 KB |
Output is correct |
12 |
Correct |
8 ms |
11848 KB |
Output is correct |
13 |
Correct |
5 ms |
11848 KB |
Output is correct |
14 |
Correct |
7 ms |
11848 KB |
Output is correct |
15 |
Correct |
9 ms |
11848 KB |
Output is correct |
16 |
Correct |
12 ms |
11848 KB |
Output is correct |
17 |
Correct |
73 ms |
14468 KB |
Output is correct |
18 |
Correct |
4 ms |
14468 KB |
Output is correct |
19 |
Correct |
5 ms |
14468 KB |
Output is correct |
20 |
Correct |
52 ms |
16304 KB |
Output is correct |
21 |
Correct |
5 ms |
16304 KB |
Output is correct |
22 |
Correct |
5 ms |
16304 KB |
Output is correct |
23 |
Correct |
6 ms |
16304 KB |
Output is correct |
24 |
Correct |
5 ms |
16304 KB |
Output is correct |
25 |
Correct |
4 ms |
16304 KB |
Output is correct |
26 |
Correct |
5 ms |
16304 KB |
Output is correct |
27 |
Correct |
4 ms |
16304 KB |
Output is correct |
28 |
Correct |
5 ms |
16304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
18236 KB |
Output is correct |
2 |
Incorrect |
99 ms |
23284 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |