Submission #156126

# Submission time Handle Problem Language Result Execution time Memory
156126 2019-10-03T14:55:51 Z Flying_dragon_02 우호 조약 체결 (JOI14_friends) C++14
100 / 100
133 ms 18424 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define fi first
#define se second
#define pb push_back
#define mp make_pair
 
typedef pair<int, int> ii;
 
const int mod = 1e9 + 7;
 
const int inf = 1e9;
 
int add(int x, int y) {
    return (1ll * x + 1ll * y) % mod;
}
 
int del(int x, int y) {
    return ((1ll * x - 1ll * y) % mod + mod) % mod;
}
 
int mul(int x, int y) {
    return (1ll * x * 1ll * y) % mod;
}
 
const int N = 2e5 + 5;
 
int n, m, pSet[N], sz[N];
 
vector<int> graph[N];
 
int findSet(int u) {
    //cout << u << "\n";
    if(u == pSet[u]) return u;
    else return pSet[u] = findSet(pSet[u]);
}
 
void unionSet(int u, int v) {
    u = findSet(u), v = findSet(v);
    if(u == v) return ;
    sz[v] += sz[u];
    pSet[u] = v;
}
 
bool vis[N];
 
vector<int> vec, gr[N];
 
queue<int> q;
 
void dfs() {
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i];
            if(!vis[v]) {
                vis[v] = 1;
                q.push(v);
            }
            unionSet(u, v);
        }
    }
}
 
int main() {
    cin.tie(0), ios_base::sync_with_stdio(0);
    //freopen("test.inp", "r", stdin);
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        pSet[i] = i, sz[i] = 1;
    for(int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].pb(v);
    }
    for(int i = 1; i <= n; i++) {
        if(graph[i].size() >= 2) {
            for(int j = 0; j < graph[i].size() - 1; j++)
                unionSet(graph[i][j], graph[i][j + 1]);
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < graph[i].size(); j++) {
            int v = graph[i][j];
            if(findSet(i) != findSet(v))
                gr[findSet(i)].pb(findSet(v));
        }
    }
    for(int i = 1; i <= n; i++) {
        graph[i] = gr[i];
        gr[i].clear();
    }
    for(int i = 1; i <= n; i++) {
        if(!vis[findSet(i)] && sz[findSet(i)] >= 2)
            q.push(findSet(i)), vis[findSet(i)] = 1;
    }
    dfs();
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < graph[i].size(); j++) {
            int v = graph[i][j];
            if(findSet(i) != findSet(v))
                gr[findSet(i)].pb(findSet(v));
        }
    }
    for(int i = 1; i <= n; i++)
        graph[i] = gr[i];
    memset(vis, 0, sizeof(vis));
    long long total = 0;
    for(int i = 1; i <= n; i++) {
        if(!vis[findSet(i)]) {
            vis[findSet(i)] = 1;
            if(sz[findSet(i)] >= 2)
                total += 1ll * sz[findSet(i)] * (sz[findSet(i)] - 1);
            else
                total += graph[findSet(i)].size();
        }
    }
    cout << total;
}

Compilation message

friends.cpp: In function 'void dfs()':
friends.cpp:57:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < graph[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~~
friends.cpp: In function 'int main()':
friends.cpp:81:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j < graph[i].size() - 1; j++)
                            ~~^~~~~~~~~~~~~~~~~~~~~
friends.cpp:86:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < graph[i].size(); j++) {
                        ~~^~~~~~~~~~~~~~~~~
friends.cpp:102:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < graph[i].size(); j++) {
                        ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9976 KB Output is correct
2 Correct 11 ms 9976 KB Output is correct
3 Correct 10 ms 9976 KB Output is correct
4 Correct 11 ms 9976 KB Output is correct
5 Correct 10 ms 9976 KB Output is correct
6 Correct 10 ms 9976 KB Output is correct
7 Correct 11 ms 9976 KB Output is correct
8 Correct 10 ms 9976 KB Output is correct
9 Correct 11 ms 9976 KB Output is correct
10 Correct 11 ms 9976 KB Output is correct
11 Correct 11 ms 9976 KB Output is correct
12 Correct 11 ms 9976 KB Output is correct
13 Correct 11 ms 9976 KB Output is correct
14 Correct 11 ms 9976 KB Output is correct
15 Correct 10 ms 9976 KB Output is correct
16 Correct 11 ms 9976 KB Output is correct
17 Correct 11 ms 9976 KB Output is correct
18 Correct 11 ms 9976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 10232 KB Output is correct
2 Correct 11 ms 10104 KB Output is correct
3 Correct 18 ms 10232 KB Output is correct
4 Correct 49 ms 10920 KB Output is correct
5 Correct 42 ms 10744 KB Output is correct
6 Correct 64 ms 11460 KB Output is correct
7 Correct 13 ms 10024 KB Output is correct
8 Correct 14 ms 9976 KB Output is correct
9 Correct 18 ms 10232 KB Output is correct
10 Correct 26 ms 10360 KB Output is correct
11 Correct 64 ms 11356 KB Output is correct
12 Correct 16 ms 10104 KB Output is correct
13 Correct 13 ms 10232 KB Output is correct
14 Correct 12 ms 10104 KB Output is correct
15 Correct 14 ms 10108 KB Output is correct
16 Correct 21 ms 10232 KB Output is correct
17 Correct 62 ms 11256 KB Output is correct
18 Correct 15 ms 10360 KB Output is correct
19 Correct 13 ms 10360 KB Output is correct
20 Correct 64 ms 11384 KB Output is correct
21 Correct 13 ms 10232 KB Output is correct
22 Correct 13 ms 10232 KB Output is correct
23 Correct 15 ms 10104 KB Output is correct
24 Correct 13 ms 10104 KB Output is correct
25 Correct 13 ms 10236 KB Output is correct
26 Correct 13 ms 10232 KB Output is correct
27 Correct 13 ms 10232 KB Output is correct
28 Correct 13 ms 10232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 11128 KB Output is correct
2 Correct 118 ms 13944 KB Output is correct
3 Correct 117 ms 14072 KB Output is correct
4 Correct 92 ms 14456 KB Output is correct
5 Correct 48 ms 13304 KB Output is correct
6 Correct 96 ms 12364 KB Output is correct
7 Correct 91 ms 14204 KB Output is correct
8 Correct 95 ms 14248 KB Output is correct
9 Correct 68 ms 14204 KB Output is correct
10 Correct 83 ms 14072 KB Output is correct
11 Correct 121 ms 14260 KB Output is correct
12 Correct 123 ms 14476 KB Output is correct
13 Correct 94 ms 17016 KB Output is correct
14 Correct 70 ms 11512 KB Output is correct
15 Correct 48 ms 13308 KB Output is correct
16 Correct 15 ms 10872 KB Output is correct
17 Correct 14 ms 10744 KB Output is correct
18 Correct 111 ms 13560 KB Output is correct
19 Correct 114 ms 13560 KB Output is correct
20 Correct 79 ms 13432 KB Output is correct
21 Correct 51 ms 12792 KB Output is correct
22 Correct 14 ms 10744 KB Output is correct
23 Correct 38 ms 10616 KB Output is correct
24 Correct 79 ms 13176 KB Output is correct
25 Correct 83 ms 16940 KB Output is correct
26 Correct 94 ms 18424 KB Output is correct
27 Correct 133 ms 14712 KB Output is correct
28 Correct 99 ms 15336 KB Output is correct
29 Correct 93 ms 15456 KB Output is correct
30 Correct 130 ms 14224 KB Output is correct
31 Correct 92 ms 13940 KB Output is correct
32 Correct 81 ms 16928 KB Output is correct
33 Correct 91 ms 17016 KB Output is correct
34 Correct 89 ms 14968 KB Output is correct
35 Correct 79 ms 17144 KB Output is correct
36 Correct 90 ms 18040 KB Output is correct
37 Correct 86 ms 15472 KB Output is correct