답안 #42338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42338 2018-02-26T08:48:46 Z minhtung0404 우호 조약 체결 (JOI14_friends) C++14
100 / 100
136 ms 55072 KB
//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];
long long 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 < (int)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 < (int)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 += 1LL * 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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 4 ms 2836 KB Output is correct
4 Correct 3 ms 2836 KB Output is correct
5 Correct 3 ms 2836 KB Output is correct
6 Correct 3 ms 2836 KB Output is correct
7 Correct 3 ms 2836 KB Output is correct
8 Correct 4 ms 2836 KB Output is correct
9 Correct 3 ms 2852 KB Output is correct
10 Correct 3 ms 2852 KB Output is correct
11 Correct 3 ms 2904 KB Output is correct
12 Correct 3 ms 2904 KB Output is correct
13 Correct 3 ms 2904 KB Output is correct
14 Correct 3 ms 2908 KB Output is correct
15 Correct 3 ms 2908 KB Output is correct
16 Correct 3 ms 2908 KB Output is correct
17 Correct 4 ms 2980 KB Output is correct
18 Correct 4 ms 2980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3036 KB Output is correct
2 Correct 5 ms 3036 KB Output is correct
3 Correct 13 ms 3164 KB Output is correct
4 Correct 63 ms 3932 KB Output is correct
5 Correct 31 ms 3932 KB Output is correct
6 Correct 60 ms 4444 KB Output is correct
7 Correct 4 ms 4444 KB Output is correct
8 Correct 6 ms 4444 KB Output is correct
9 Correct 10 ms 4444 KB Output is correct
10 Correct 16 ms 4444 KB Output is correct
11 Correct 50 ms 4448 KB Output is correct
12 Correct 7 ms 4448 KB Output is correct
13 Correct 4 ms 4448 KB Output is correct
14 Correct 4 ms 4448 KB Output is correct
15 Correct 6 ms 4448 KB Output is correct
16 Correct 12 ms 4448 KB Output is correct
17 Correct 48 ms 4448 KB Output is correct
18 Correct 4 ms 4448 KB Output is correct
19 Correct 4 ms 4448 KB Output is correct
20 Correct 51 ms 4448 KB Output is correct
21 Correct 4 ms 4448 KB Output is correct
22 Correct 4 ms 4448 KB Output is correct
23 Correct 6 ms 4448 KB Output is correct
24 Correct 5 ms 4448 KB Output is correct
25 Correct 4 ms 4448 KB Output is correct
26 Correct 4 ms 4448 KB Output is correct
27 Correct 4 ms 4448 KB Output is correct
28 Correct 4 ms 4448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 4448 KB Output is correct
2 Correct 136 ms 6892 KB Output is correct
3 Correct 91 ms 9300 KB Output is correct
4 Correct 46 ms 9500 KB Output is correct
5 Correct 25 ms 9500 KB Output is correct
6 Correct 74 ms 11988 KB Output is correct
7 Correct 68 ms 15632 KB Output is correct
8 Correct 78 ms 18080 KB Output is correct
9 Correct 41 ms 18388 KB Output is correct
10 Correct 58 ms 20688 KB Output is correct
11 Correct 87 ms 23368 KB Output is correct
12 Correct 84 ms 26056 KB Output is correct
13 Correct 36 ms 26764 KB Output is correct
14 Correct 54 ms 26764 KB Output is correct
15 Correct 21 ms 27588 KB Output is correct
16 Correct 5 ms 27588 KB Output is correct
17 Correct 4 ms 27588 KB Output is correct
18 Correct 85 ms 31276 KB Output is correct
19 Correct 83 ms 33580 KB Output is correct
20 Correct 46 ms 33852 KB Output is correct
21 Correct 23 ms 33852 KB Output is correct
22 Correct 5 ms 33852 KB Output is correct
23 Correct 28 ms 33852 KB Output is correct
24 Correct 58 ms 36644 KB Output is correct
25 Correct 38 ms 39100 KB Output is correct
26 Correct 40 ms 41824 KB Output is correct
27 Correct 96 ms 43088 KB Output is correct
28 Correct 41 ms 43248 KB Output is correct
29 Correct 52 ms 44360 KB Output is correct
30 Correct 115 ms 47360 KB Output is correct
31 Correct 55 ms 47360 KB Output is correct
32 Correct 45 ms 49380 KB Output is correct
33 Correct 46 ms 50524 KB Output is correct
34 Correct 50 ms 50876 KB Output is correct
35 Correct 39 ms 52896 KB Output is correct
36 Correct 39 ms 55072 KB Output is correct
37 Correct 39 ms 55072 KB Output is correct