QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#709622#3556. Making Friends on Joitter is Funrtgsp0 20ms145420kbC++202.0kb2024-11-04 15:55:232024-11-04 15:55:25

Judging History

This is the latest submission verdict.

  • [2024-11-04 15:55:25]
  • Judged
  • Verdict: 0
  • Time: 20ms
  • Memory: 145420kb
  • [2024-11-04 15:55:23]
  • Submitted

answer

#include<bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const ll maxn = 1e6 + 2, LG = 20, blocksize = 550, inf = 1e18;
int n, m, a, b, par[maxn];
ll res = 0;
set<int> f[maxn], adj[maxn], radj[maxn];
vector<pair<int, int>> cur;
int find_set (int u)
{
    if (par[u] < 0) return u;
    return (par[u] = find_set(par[u]));
}
void union_set (int u, int v)
{
    if (u == v) return;
    if (f[u].size() + adj[u].size() + radj[u].size() < f[v].size() + adj[v].size() + radj[v].size()) swap(u, v);
    res -= 1LL*par[u]*f[v].size() + 1LL*par[v]*f[u].size();
    par[u] += par[v];
    par[v] = u;
    for (int i : f[v])
    {
        if (f[u].find(i) != f[u].end()) res += par[u];
        else f[u].insert(i);
    }
    f[v].clear();
    adj[u].erase(v); radj[u].erase(v);
    adj[v].erase(u); radj[v].erase(u);
    for (int i : adj[v])
    {
        adj[u].insert(i);
        radj[i].insert(u);
        if (radj[u].find(i) != radj[u].end()) cur.push_back({u, i});
    }
    for (int i : radj[v])
    {
        radj[u].insert(i);
        adj[i].insert(u);
        if (adj[u].find(i) != adj[u].end()) cur.push_back({u, i});
    }
}
int main()
{
    //freopen(task".INP", "r", stdin);
    //freopen(task".OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        par[i] = -1;
        f[i].insert(i);
    }
    while (m--)
    {
        cin >> a >> b;
        b = find_set(b);
        if (a == b || f[b].find(a) != f[b].end())
        {
            cout << res << '\n';
            continue;
        }
        res -= par[b];
        f[b].insert(a);
        a = find_set(a);
        adj[a].insert(b);
        radj[b].insert(a);
        if (radj[a].find(b) != radj[a].end()) cur.push_back({a, b});
        while (!cur.empty())
        {
            union_set(cur[0].first, cur[0].second);
            swap(cur[0], cur.back());
            cur.pop_back();
        }
        cout << res << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 1
Accepted
time: 11ms
memory: 145200kb

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: 145204kb

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: 145176kb

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: 19ms
memory: 145420kb

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: 0
Time Limit Exceeded

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:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%