QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#116910#3556. Making Friends on Joitter is Funminhcool0 31ms222260kbC++172.6kb2023-06-30 10:31:332023-06-30 10:31:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-30 10:31:35]
  • 评测
  • 测评结果:0
  • 用时:31ms
  • 内存:222260kb
  • [2023-06-30 10:31:33]
  • 提交

answer

#define local
#ifndef local
#include ""
#endif
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
 
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
 
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
 
const int N = 1e6 + 5;
 
const int oo = 1e18 + 7, mod = 1e9 + 7;
 
mt19937 rng(1);
 
int rnd(int l, int r){
	int temp = rng() % (r - l + 1);
	return abs(temp) + l;
}
 
int n, m;
set<ii> edges;
 
unordered_set<int> to[N], from[N];// to includes all nodes that are inside to
unordered_set<int> tocomp[N], fromcomp[N];
int rt[N], sz[N];
 
int answer;
 
int root(int x){
    return (x == rt[x] ? x : rt[x] = root(rt[x]));
}

queue<ii> q;
 
void merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return;
    //cout << x << " " << y << "\n";
    if(sz[x] < sz[y]) swap(x, y);
    answer -= sz[x] * (to[x].size() - sz[x]);
    answer -= (sz[x] * (sz[x] - 1));
    answer -= sz[y] * (to[y].size() - sz[y]);
    answer -= (sz[y] * (sz[y] - 1));
    rt[y] = x;
    sz[x] += sz[y];
    //for(auto it : to[y]) to[x].insert(it);
    for(auto it : from[y]) from[x].insert(it);
    for(auto it : to[y]) to[x].insert(it);
    for(auto it : fromcomp[y]){
        fromcomp[x].insert(it);
        tocomp[it].insert(x);
    }
    for(auto it : tocomp[y]){
        tocomp[x].insert(it);
        fromcomp[it].insert(x);
    }
    for(auto it : tocomp[y]) if(tocomp[it].find(x) != tocomp[it].end()) q.push({x, it});
    from[y].clear();
    to[y].clear();
    tocomp[y].clear();
    fromcomp[y].clear();
    answer += sz[x] * (to[x].size() - sz[x]);
    answer += sz[x] * (sz[x] - 1);
}
 
#ifdef local
void process(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
	    rt[i] = i, sz[i] = 1, to[i].insert(i), from[i].insert(i), tocomp[i].insert(i), fromcomp[i].insert(i);
	}
	for(int i = 1; i <= m; i++){
	    int x, y;
	    cin >> x >> y;
	    int xx = root(x), yy = root(y);
	    if(xx != yy){
	        if(to[yy].find(x) == to[yy].end()){
	            to[yy].insert(x);
	            answer += sz[yy];
	        }
	        from[xx].insert(y);
	        fromcomp[xx].insert(yy);
	        tocomp[yy].insert(xx);
	        if(fromcomp[yy].find(xx) != fromcomp[yy].end()) q.push({xx, yy});
	    }
	    while(!q.empty()){
	        merge(q.front().fi, q.front().se);
	        q.pop();
	    }
	    //edges.insert({x, yy});
	    cout << answer << "\n";
	}
}
 
signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	process();
}
#endif

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 1
Accepted
time: 23ms
memory: 222260kb

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: 0
Accepted
time: 28ms
memory: 222176kb

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
Wrong Answer
time: 31ms
memory: 222252kb

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
10
15
20
20
20
20
20
20
20
20
20
20

result:

wrong answer 9th lines differ - expected: '11', found: '10'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%