Submission #156090

# Submission time Handle Problem Language Result Execution time Memory
156090 2019-10-03T08:47:21 Z atoiz 우호 조약 체결 (JOI14_friends) C++14
100 / 100
163 ms 12664 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 100007;

int n, m;
vector<int> gr[MAXN];
bool vis[MAXN];

int dsu[MAXN], sz[MAXN];
void init(int n) { for (int i = 0; i < n; ++i) dsu[i] = i, sz[i] = 1; }
int get(int i) { return (i == dsu[i] ? i : dsu[i] = get(dsu[i])); }
bool same(int i, int j) { return get(i) == get(j); }
void join(int i, int j) { i = get(i), j = get(j); if (i == j) return; if (sz[i] < sz[j]) swap(i, j); sz[i] += sz[j], dsu[j] = i; }

void dfs(int);
void add(int u, int v)
{
    bool redfs = (!vis[v] || sz[get(v)] == 1);
    // cerr << "S " << u << ' ' << v << ' ' << redfs << endl;
    join(u, v);
    if (redfs) dfs(v);
}
void dfs(int u)
{
    // cerr << u << ": " << sz[get(u)] << endl;
    vis[u] = 1;
    if (sz[get(u)] > 1) for (int v : gr[u]) add(u, v);
    else if ((int) gr[u].size() == 1) {
        if (!vis[gr[u][0]]) dfs(gr[u][0]);
    } else if ((int) gr[u].size() > 1) {
        bool redfs = sz[gr[u].back()] == 1;
        add(gr[u].back(), gr[u].front());
        for (int v : gr[u]) if (v != gr[u].front()) add(gr[u].front(), v);
        if (redfs) dfs(gr[u].back());
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        gr[u].push_back(v);
    }
    init(n + 1);
    for (int u = 1; u <= n; ++u) if (!vis[u]) dfs(u);

    long long ans = 0;
    for (int u = 1; u <= n; ++u) for (int v : gr[u]) {
        if (!same(u, v)) ++ans;
    }
    for (int u = 1; u <= n; ++u) if (u == get(u)) ans += 1ll * sz[u] * (sz[u] - 1);
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 5 ms 2680 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2680 KB Output is correct
10 Correct 5 ms 2680 KB Output is correct
11 Correct 5 ms 2680 KB Output is correct
12 Correct 5 ms 2680 KB Output is correct
13 Correct 5 ms 2680 KB Output is correct
14 Correct 5 ms 2680 KB Output is correct
15 Correct 5 ms 2680 KB Output is correct
16 Correct 5 ms 2680 KB Output is correct
17 Correct 5 ms 2680 KB Output is correct
18 Correct 5 ms 2708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2808 KB Output is correct
2 Correct 6 ms 2808 KB Output is correct
3 Correct 12 ms 3320 KB Output is correct
4 Correct 44 ms 5284 KB Output is correct
5 Correct 37 ms 4728 KB Output is correct
6 Correct 59 ms 6264 KB Output is correct
7 Correct 5 ms 2680 KB Output is correct
8 Correct 7 ms 2936 KB Output is correct
9 Correct 12 ms 3196 KB Output is correct
10 Correct 19 ms 3832 KB Output is correct
11 Correct 59 ms 6392 KB Output is correct
12 Correct 9 ms 3192 KB Output is correct
13 Correct 6 ms 2936 KB Output is correct
14 Correct 6 ms 2808 KB Output is correct
15 Correct 8 ms 2936 KB Output is correct
16 Correct 15 ms 3448 KB Output is correct
17 Correct 58 ms 6136 KB Output is correct
18 Correct 6 ms 2936 KB Output is correct
19 Correct 6 ms 3192 KB Output is correct
20 Correct 57 ms 6264 KB Output is correct
21 Correct 6 ms 2936 KB Output is correct
22 Correct 6 ms 2936 KB Output is correct
23 Correct 8 ms 3064 KB Output is correct
24 Correct 7 ms 2808 KB Output is correct
25 Correct 7 ms 2936 KB Output is correct
26 Correct 6 ms 2936 KB Output is correct
27 Correct 7 ms 2936 KB Output is correct
28 Correct 7 ms 2936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 3964 KB Output is correct
2 Correct 127 ms 7620 KB Output is correct
3 Correct 139 ms 9848 KB Output is correct
4 Correct 73 ms 6648 KB Output is correct
5 Correct 38 ms 5496 KB Output is correct
6 Correct 101 ms 8696 KB Output is correct
7 Correct 105 ms 8876 KB Output is correct
8 Correct 74 ms 8696 KB Output is correct
9 Correct 56 ms 6904 KB Output is correct
10 Correct 75 ms 7804 KB Output is correct
11 Correct 148 ms 9976 KB Output is correct
12 Correct 163 ms 10956 KB Output is correct
13 Correct 62 ms 7800 KB Output is correct
14 Correct 61 ms 6648 KB Output is correct
15 Correct 34 ms 5356 KB Output is correct
16 Correct 7 ms 3704 KB Output is correct
17 Correct 6 ms 3576 KB Output is correct
18 Correct 101 ms 9020 KB Output is correct
19 Correct 118 ms 8956 KB Output is correct
20 Correct 55 ms 6392 KB Output is correct
21 Correct 28 ms 5240 KB Output is correct
22 Correct 6 ms 3576 KB Output is correct
23 Correct 31 ms 4600 KB Output is correct
24 Correct 56 ms 6508 KB Output is correct
25 Correct 46 ms 7780 KB Output is correct
26 Correct 53 ms 12664 KB Output is correct
27 Correct 99 ms 11356 KB Output is correct
28 Correct 48 ms 7032 KB Output is correct
29 Correct 48 ms 7032 KB Output is correct
30 Correct 93 ms 9976 KB Output is correct
31 Correct 51 ms 6328 KB Output is correct
32 Correct 42 ms 7800 KB Output is correct
33 Correct 40 ms 7800 KB Output is correct
34 Correct 51 ms 6776 KB Output is correct
35 Correct 47 ms 7928 KB Output is correct
36 Correct 47 ms 9592 KB Output is correct
37 Correct 44 ms 7032 KB Output is correct