답안 #384081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
384081 2021-03-31T11:45:08 Z VodkaInTheJar 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
17 / 100
5000 ms 19672 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

const int maxn = 1e5 + 3; 

int n, m; 
void read()
{
	cin >> n >> m; 
}

int par[maxn], sz[maxn];
set <int> adj[maxn], rev_adj[maxn];
int find_root(int ver)
{
    if (ver == par[ver])
        return ver;

    return par[ver] = find_root(par[ver]);
}

void merge(int a, int b)
{
    a = find_root(a), b = find_root(b);
    
    if (a == b)
        return;

    if (sz[a] < sz[b])
        swap(a, b);

    for (auto j: adj[b])
        if (j != a)
        adj[a].insert(j);

    for (auto j: rev_adj[b])
    {
        if (find_root(j) == a)
            continue;

        adj[find_root(j)].erase(b);
        adj[find_root(j)].insert(a);
        rev_adj[a].insert(j);
    }
    
    adj[a].erase(b);
    vector <int> to_remove;
    for (auto i: rev_adj[a])
        if (find_root(i) == b)
            to_remove.push_back(i);

    for (auto i: to_remove)
        rev_adj[a].erase(i);
    
    /*
    cout << a << ":";
    for (auto i: adj[a])
        cout << i << " ";

    cout << endl;
    cout << endl;
    for (auto i: rev_adj[a])
        cout << i << " ";
 //   cout << endl;
   // cout << rev_adj[1].size() << endl; 
  //  cout << endl;
    exit(0);
    */


    par[b] = a;
    sz[a] += sz[b];
}

pair <int, int> e[maxn * 3];
long long find_ans(int idx)
{
    long long ans = 0; 
    for (int i = 1; i <= n; i++)
        if (i == par[i])
        {
            ans += 1ll * sz[i] * (sz[i] - 1);
            ans += 1ll * (int)rev_adj[i].size() * sz[i];
        }

    return ans; 
}

void solve()
{
    for (int i = 1; i <= n; i++)
        par[i] = i, sz[i] = 1; 

    for (int i = 1; i <= m; i++)
    {
        cin >> e[i].first >> e[i].second;
        
        int a = find_root(e[i].first), b = find_root(e[i].second);
        if (a != b)
        {
            adj[a].insert(b);
            rev_adj[b].insert(e[i].first);

            int curr = a;
            for (;;)
            {
                bool can = false;
                for (auto j: adj[curr])
                    if (adj[j].find(curr) != adj[j].end())
                    {
                        merge(j, curr);
                        curr = find_root(curr);
                        can = true;
                        break;
                    }

                if (!can)
                    break;
            }
        }

        //cout << i << "shit" << (int)rev_adj[2].size() << endl;
        cout << find_ans(i) << endl; 
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	read();
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9732 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 8 ms 9836 KB Output is correct
8 Correct 9 ms 9836 KB Output is correct
9 Correct 9 ms 9836 KB Output is correct
10 Correct 8 ms 9708 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 8 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 8 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 8 ms 9708 KB Output is correct
21 Correct 8 ms 9836 KB Output is correct
22 Correct 7 ms 9708 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 8 ms 9708 KB Output is correct
25 Correct 8 ms 9836 KB Output is correct
26 Correct 7 ms 9708 KB Output is correct
27 Correct 7 ms 9708 KB Output is correct
28 Correct 7 ms 9708 KB Output is correct
29 Correct 7 ms 9708 KB Output is correct
30 Correct 7 ms 9708 KB Output is correct
31 Correct 8 ms 9836 KB Output is correct
32 Correct 7 ms 9708 KB Output is correct
33 Correct 8 ms 9836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9732 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 8 ms 9836 KB Output is correct
8 Correct 9 ms 9836 KB Output is correct
9 Correct 9 ms 9836 KB Output is correct
10 Correct 8 ms 9708 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 8 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 8 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 8 ms 9708 KB Output is correct
21 Correct 8 ms 9836 KB Output is correct
22 Correct 7 ms 9708 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 8 ms 9708 KB Output is correct
25 Correct 8 ms 9836 KB Output is correct
26 Correct 7 ms 9708 KB Output is correct
27 Correct 7 ms 9708 KB Output is correct
28 Correct 7 ms 9708 KB Output is correct
29 Correct 7 ms 9708 KB Output is correct
30 Correct 7 ms 9708 KB Output is correct
31 Correct 8 ms 9836 KB Output is correct
32 Correct 7 ms 9708 KB Output is correct
33 Correct 8 ms 9836 KB Output is correct
34 Correct 12 ms 9964 KB Output is correct
35 Correct 418 ms 17644 KB Output is correct
36 Correct 774 ms 19436 KB Output is correct
37 Correct 773 ms 19436 KB Output is correct
38 Correct 765 ms 19308 KB Output is correct
39 Correct 28 ms 10092 KB Output is correct
40 Correct 28 ms 10092 KB Output is correct
41 Correct 36 ms 10092 KB Output is correct
42 Correct 27 ms 10092 KB Output is correct
43 Correct 53 ms 10092 KB Output is correct
44 Correct 50 ms 10092 KB Output is correct
45 Correct 27 ms 10092 KB Output is correct
46 Correct 27 ms 10092 KB Output is correct
47 Correct 30 ms 10220 KB Output is correct
48 Correct 29 ms 10092 KB Output is correct
49 Correct 60 ms 10860 KB Output is correct
50 Correct 787 ms 19672 KB Output is correct
51 Correct 77 ms 10348 KB Output is correct
52 Correct 735 ms 18436 KB Output is correct
53 Correct 62 ms 10668 KB Output is correct
54 Correct 811 ms 19020 KB Output is correct
55 Correct 39 ms 10348 KB Output is correct
56 Correct 39 ms 10348 KB Output is correct
57 Correct 37 ms 10220 KB Output is correct
58 Correct 36 ms 10348 KB Output is correct
59 Correct 28 ms 10092 KB Output is correct
60 Correct 706 ms 17272 KB Output is correct
61 Correct 34 ms 10220 KB Output is correct
62 Correct 768 ms 19436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9732 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 8 ms 9836 KB Output is correct
8 Correct 9 ms 9836 KB Output is correct
9 Correct 9 ms 9836 KB Output is correct
10 Correct 8 ms 9708 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 8 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 8 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 8 ms 9708 KB Output is correct
21 Correct 8 ms 9836 KB Output is correct
22 Correct 7 ms 9708 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 8 ms 9708 KB Output is correct
25 Correct 8 ms 9836 KB Output is correct
26 Correct 7 ms 9708 KB Output is correct
27 Correct 7 ms 9708 KB Output is correct
28 Correct 7 ms 9708 KB Output is correct
29 Correct 7 ms 9708 KB Output is correct
30 Correct 7 ms 9708 KB Output is correct
31 Correct 8 ms 9836 KB Output is correct
32 Correct 7 ms 9708 KB Output is correct
33 Correct 8 ms 9836 KB Output is correct
34 Correct 12 ms 9964 KB Output is correct
35 Correct 418 ms 17644 KB Output is correct
36 Correct 774 ms 19436 KB Output is correct
37 Correct 773 ms 19436 KB Output is correct
38 Correct 765 ms 19308 KB Output is correct
39 Correct 28 ms 10092 KB Output is correct
40 Correct 28 ms 10092 KB Output is correct
41 Correct 36 ms 10092 KB Output is correct
42 Correct 27 ms 10092 KB Output is correct
43 Correct 53 ms 10092 KB Output is correct
44 Correct 50 ms 10092 KB Output is correct
45 Correct 27 ms 10092 KB Output is correct
46 Correct 27 ms 10092 KB Output is correct
47 Correct 30 ms 10220 KB Output is correct
48 Correct 29 ms 10092 KB Output is correct
49 Correct 60 ms 10860 KB Output is correct
50 Correct 787 ms 19672 KB Output is correct
51 Correct 77 ms 10348 KB Output is correct
52 Correct 735 ms 18436 KB Output is correct
53 Correct 62 ms 10668 KB Output is correct
54 Correct 811 ms 19020 KB Output is correct
55 Correct 39 ms 10348 KB Output is correct
56 Correct 39 ms 10348 KB Output is correct
57 Correct 37 ms 10220 KB Output is correct
58 Correct 36 ms 10348 KB Output is correct
59 Correct 28 ms 10092 KB Output is correct
60 Correct 706 ms 17272 KB Output is correct
61 Correct 34 ms 10220 KB Output is correct
62 Correct 768 ms 19436 KB Output is correct
63 Execution timed out 5044 ms 13204 KB Time limit exceeded
64 Halted 0 ms 0 KB -