QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710053#3556. Making Friends on Joitter is FuncpismylifeOwO0 44ms200264kbC++203.7kb2024-11-04 18:17:252024-11-04 18:17:25

Judging History

你现在查看的是最新测评结果

  • [2024-11-04 18:17:25]
  • 评测
  • 测评结果:0
  • 用时:44ms
  • 内存:200264kb
  • [2024-11-04 18:17:25]
  • 提交

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];

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];
        }
    }
    sz[a] += sz[b];
}

void Exc()
{
    for (int x = 1; x <= n; x++)
    {
        parents[x] = x;
        sz[x] = 1;
    }
    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();
            UnionSet(x.first, x.second);
        }
        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: 20ms
memory: 198232kb

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: 24ms
memory: 200212kb

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: 44ms
memory: 198168kb

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: 28ms
memory: 198240kb

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
Wrong Answer
time: 28ms
memory: 200264kb

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
43
43
49
50
50
50
50
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78

result:

wrong answer 18th lines differ - expected: '52', found: '43'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%