QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710781#3556. Making Friends on Joitter is FuncpismylifeOwO0 37ms200304kbC++204.1kb2024-11-04 21:44:222024-11-04 21:44:27

Judging History

This is the latest submission verdict.

  • [2024-11-04 21:44:27]
  • Judged
  • Verdict: 0
  • Time: 37ms
  • Memory: 200304kb
  • [2024-11-04 21:44:22]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

const long long mod = 1e9 + 7;
const int MaxN = 1e6 + 5;

struct Edge
{
    int u, v;
};

int n, m;
Edge edges[MaxN];
bool debug;

void Inp()
{
    cin >> n >> m;
    for (int x = 1; x <= m; x++)
    {
        cin >> edges[x].u >> edges[x].v;
    }
}

long long cur;
int parents[MaxN];
int sz[MaxN];
int sum[MaxN];
set<int> graph[MaxN];
set<int> rev[MaxN];
set<int> graphs[MaxN];
map<int, int> revs[MaxN];
vector<int> listing[MaxN];
vector<pair<int, int>> tmp;

int FindSet(int p)
{
    if (parents[p] == p)
    {
        return parents[p];
    }
    parents[p] = FindSet(parents[p]);
    return parents[p];
}

void UnionSet(int a, int b)
{
    a = FindSet(a);
    b = FindSet(b);
    if (a == b)
    {
        return;
    }
    if (sz[a] < sz[b])
    {
        swap(a, b);
    }
    parents[b] = a;
    cur += 2ll * sz[a] * sz[b];
    graph[a].erase(b);
    rev[a].erase(b);
    graph[b].erase(a);
    rev[b].erase(a);
    for (int x : graph[b])
    {
        if (rev[a].find(x) != rev[a].end())
        {
            tmp.push_back(make_pair(x, a));
        }
        rev[x].erase(b);
        rev[x].insert(a);
    }
    for (int x : rev[b])
    {
        if (graph[a].find(x) != graph[a].end())
        {
            tmp.push_back(make_pair(x, a));
        }
        graph[x].erase(b);
        graph[x].insert(a);
    }
    cur += 1ll * sum[a] * sz[b];
    for (pair<int, int> x : revs[b])
    {
        if (FindSet(x.first) == a)
        {
            continue;
        }
        auto p = revs[a].find(x.first);
        if (p == revs[a].end())
        {
            cur += sz[a];
        }
        else
        {
            graphs[x.first].erase(x.second);
            cur -= sz[b];
        }
    }
    for (int x : graph[b])
    {
        graph[a].insert(x);
    }
    for (int x : rev[b])
    {
        rev[a].insert(x);
    }
    //cout << sum[a] << " ";
    for (int x : graphs[b])
    {
        if (FindSet(x) != a)
        {
            graphs[a].insert(x);
        }
        else
        {
            sum[a]--;
            cur -= sz[a] + sz[b];
        }
        //cout << x << " ";
    }
    for (pair<int, int> x : revs[b])
    {
        if (FindSet(x.first) != a && revs[a].find(x.first) == revs[a].end())
        {
            revs[a].insert(x);
            sum[a]++;
        }
        else if (FindSet(x.first) == a)
        {
            cur -= sz[b];
        }
    }
    for (int x : listing[b])
    {
        revs[a].erase(x);
        graphs[a].erase(x);
        listing[a].push_back(b);
    }
    listing[b].clear();
    sz[a] += sz[b];
}

void Exc()
{
    for (int x = 1; x <= n; x++)
    {
        parents[x] = x;
        sz[x] = 1;
        listing[x].push_back(x);
    }
    for (int x = 1; x <= m; x++)
    {
        int u = FindSet(edges[x].u), v = FindSet(edges[x].v);
        if (u == v)
        {
            cout << cur << "\n";
            continue;
        }
        if (revs[v].find(edges[x].u) != revs[v].end())
        {
            cout << cur << "\n";
            continue;
        }
        revs[v].insert(make_pair(edges[x].u, edges[x].v));
        graphs[u].insert(edges[x].v);
        sum[v]++;
        cur += sz[v];
        rev[v].insert(u);
        graph[u].insert(v);
        if (rev[u].find(v) != rev[u].end())
        {
            tmp.push_back(make_pair(u, v));
        }
        while (!tmp.empty())
        {
            pair<int, int> x = tmp.back();
            tmp.pop_back();
            if (x.first == 10 && x.second == 8)
            {
                debug = true;
            }
            UnionSet(x.first, x.second);
            debug = false;
        }
        cout << cur << "\n";
    }
}

int main()
{
    //freopen("B.INP", "r", stdin);
    //freopen("B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int test = 1;
    //cin >> test;
    for (int x = 1; x <= test; x++)
    {
        Inp();
        Exc();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 1
Accepted
time: 30ms
memory: 198216kb

input:

5 20
4 2
1 5
2 3
2 5
3 2
3 1
4 5
1 2
5 2
1 4
4 1
3 4
5 1
2 4
2 1
4 3
1 3
5 4
3 5
5 3

output:

1
2
3
4
6
7
8
12
16
20
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #2:

score: 1
Accepted
time: 16ms
memory: 200204kb

input:

5 20
4 5
1 2
2 1
4 3
3 2
5 2
1 5
4 1
2 3
1 3
1 4
4 2
5 1
3 5
2 5
3 1
2 4
3 4
5 4
5 3

output:

1
2
3
4
6
8
13
13
16
16
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #3:

score: 1
Accepted
time: 20ms
memory: 200280kb

input:

5 20
3 1
5 1
3 4
5 2
1 2
5 4
3 5
2 4
1 3
2 5
4 5
4 3
4 2
2 3
3 2
5 3
2 1
1 5
4 1
1 4

output:

1
2
3
4
5
6
7
8
11
15
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #4:

score: 1
Accepted
time: 32ms
memory: 198232kb

input:

10 10
9 1
10 6
4 10
1 8
4 9
7 8
2 4
6 5
10 1
1 7

output:

1
2
3
4
5
6
7
8
9
10

result:

ok 10 lines

Test #5:

score: 1
Accepted
time: 15ms
memory: 198224kb

input:

10 90
10 6
7 8
8 4
9 3
2 8
9 2
1 10
1 8
8 9
5 6
2 10
4 3
7 2
10 2
10 5
3 7
1 9
8 7
1 2
9 1
7 6
9 7
10 3
8 3
6 3
7 5
8 2
6 1
4 9
7 1
4 2
6 8
7 3
9 8
5 1
10 4
6 4
1 4
6 7
3 1
9 10
3 2
1 7
2 5
10 1
2 7
2 1
5 8
7 9
2 4
6 9
3 8
10 7
8 5
5 4
8 6
5 10
3 5
5 7
8 1
4 7
4 1
10 8
9 5
4 8
6 10
1 6
2 9
1 5
6 5
3...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
17
18
19
20
52
52
59
60
60
60
60
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90

result:

ok 90 lines

Test #6:

score: 1
Accepted
time: 27ms
memory: 200216kb

input:

10 90
10 9
7 3
4 7
8 1
10 8
4 10
9 7
9 6
2 3
1 8
5 6
1 2
10 5
3 2
6 5
6 3
9 2
4 6
9 3
9 1
5 7
6 10
10 1
8 9
9 8
5 4
10 4
5 1
2 9
7 9
9 5
7 6
9 10
4 8
5 3
2 7
8 2
6 8
7 2
2 8
1 10
1 9
8 5
3 6
4 1
7 1
10 7
8 3
2 10
5 8
7 10
1 6
3 5
2 4
6 4
8 4
1 7
6 2
7 4
2 1
3 7
7 8
1 5
8 10
4 5
5 10
5 2
3 8
5 9
6 1
...

output:

1
2
3
4
5
6
7
8
9
11
12
13
14
17
20
22
24
26
26
28
29
35
35
49
49
55
55
55
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90

result:

ok 90 lines

Test #7:

score: 1
Accepted
time: 37ms
memory: 198192kb

input:

50 2450
21 49
31 13
28 21
35 9
40 19
8 18
8 41
13 46
5 2
46 9
30 46
37 36
4 19
23 33
28 39
2 23
38 28
13 26
46 44
29 27
35 15
10 23
49 33
2 6
16 22
2 10
29 15
18 5
15 40
46 29
18 47
31 5
9 45
18 29
15 27
40 27
12 43
14 22
8 31
50 29
16 4
35 43
36 40
16 34
28 14
30 13
9 40
44 47
33 36
10 29
26 33
8 1...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
99
100
101
102
103
106
109
110
113
114
115
116
117
118
119
120
121
12...

result:

ok 2450 lines

Test #8:

score: 0
Wrong Answer
time: 24ms
memory: 200304kb

input:

50 2450
18 43
47 16
11 47
49 15
38 20
5 6
22 9
45 25
19 31
47 41
4 12
48 13
48 43
14 44
24 36
28 31
34 2
11 8
11 22
37 30
44 1
3 8
34 29
42 47
18 5
27 8
37 34
2 45
1 24
3 31
40 2
41 46
1 47
13 18
48 49
28 27
3 46
1 4
27 21
16 42
26 5
37 4
17 50
36 8
42 29
7 14
29 5
23 32
40 29
48 11
42 28
30 18
37 1...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
...

result:

wrong answer 162nd lines differ - expected: '2076', found: '2031'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%