답안 #359857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
359857 2021-01-27T09:07:01 Z soroush 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1561 ms 144152 KB
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;
typedef pair<int , int> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 3e5 + 100;
const ll mod = 1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n, k;
ll m = 0;
int par[maxn];
vector < pii > e[maxn];
set < int > out[maxn] , in[maxn] , rlin[maxn];
set < pii > rlo[maxn];

int getpar(int v){
	return((!par[v]) ? v : par[v] = getpar(par[v]));
}

int sz[maxn];

queue < pii > q;

#define siz(x) (ll)x.size()

void merge(int v , int u){
	if(e[v].size() < e[u].size())
		swap(u , v);
	m -= sz[u] * siz(rlin[u]);
	m += sz[u] * siz(rlin[v]);
	for(int x : rlin[u])
		rlo[getpar(x)].erase({u , x});
	for(auto x : rlo[u]){
		m -= sz[x.first];
		rlin[x.first].erase(x.second);
		if(x.first == v)
			m -= sz[u];
	}
	for(auto x : e[u])
		q.push(x);
	m += sz[v] * sz[u] * 2;
	sz[v] += sz[u];
	par[u] = v;
	out[u].clear();
	in[u].clear();
	e[u].clear();	
	rlo[u].clear();
	rlin[u].clear();
}

int32_t main(){
	migmig;
	cin >> n >> k;
	for(int i = 1 ; i <= n ; i ++)sz[i] = 1;
	while(k--){
		int u , v;
		cin >> u >> v;
		q.push(pii(u , v));
		while(!q.empty()){
			int v = q.front().first , u = q.front().second;
			q.pop();
			if(getpar(v) == getpar(u))continue;
			int U = getpar(u) , V = getpar(v);
			if(out[U].count(V))
				merge(U , V);
			else{
				if(rlo[V].count(pii(U , v)))continue;
				e[V].pb({v , u}) , e[U].pb(pii(v , u));
				rlo[V].insert(pii(U , v));
				rlin[U].insert(v);
				out[V].insert(U);
				in[U].insert(V);
				m+=sz[U];
			}
		}
		cout << m << endl;
	}
	return(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 63724 KB Output is correct
2 Correct 39 ms 63836 KB Output is correct
3 Correct 39 ms 63724 KB Output is correct
4 Correct 39 ms 63724 KB Output is correct
5 Correct 41 ms 63724 KB Output is correct
6 Correct 39 ms 63852 KB Output is correct
7 Correct 41 ms 63852 KB Output is correct
8 Correct 41 ms 63852 KB Output is correct
9 Correct 41 ms 63852 KB Output is correct
10 Correct 41 ms 63724 KB Output is correct
11 Correct 40 ms 63852 KB Output is correct
12 Correct 44 ms 63724 KB Output is correct
13 Correct 41 ms 63724 KB Output is correct
14 Correct 39 ms 63724 KB Output is correct
15 Correct 40 ms 63724 KB Output is correct
16 Correct 40 ms 63852 KB Output is correct
17 Correct 39 ms 63724 KB Output is correct
18 Correct 39 ms 63724 KB Output is correct
19 Correct 41 ms 63852 KB Output is correct
20 Correct 39 ms 63852 KB Output is correct
21 Correct 40 ms 63852 KB Output is correct
22 Correct 40 ms 63852 KB Output is correct
23 Correct 40 ms 63872 KB Output is correct
24 Correct 41 ms 63800 KB Output is correct
25 Correct 47 ms 63852 KB Output is correct
26 Correct 40 ms 63852 KB Output is correct
27 Correct 42 ms 63980 KB Output is correct
28 Correct 40 ms 63852 KB Output is correct
29 Correct 41 ms 63852 KB Output is correct
30 Correct 41 ms 63872 KB Output is correct
31 Correct 40 ms 63852 KB Output is correct
32 Correct 40 ms 63724 KB Output is correct
33 Correct 40 ms 63852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 63724 KB Output is correct
2 Correct 39 ms 63836 KB Output is correct
3 Correct 39 ms 63724 KB Output is correct
4 Correct 39 ms 63724 KB Output is correct
5 Correct 41 ms 63724 KB Output is correct
6 Correct 39 ms 63852 KB Output is correct
7 Correct 41 ms 63852 KB Output is correct
8 Correct 41 ms 63852 KB Output is correct
9 Correct 41 ms 63852 KB Output is correct
10 Correct 41 ms 63724 KB Output is correct
11 Correct 40 ms 63852 KB Output is correct
12 Correct 44 ms 63724 KB Output is correct
13 Correct 41 ms 63724 KB Output is correct
14 Correct 39 ms 63724 KB Output is correct
15 Correct 40 ms 63724 KB Output is correct
16 Correct 40 ms 63852 KB Output is correct
17 Correct 39 ms 63724 KB Output is correct
18 Correct 39 ms 63724 KB Output is correct
19 Correct 41 ms 63852 KB Output is correct
20 Correct 39 ms 63852 KB Output is correct
21 Correct 40 ms 63852 KB Output is correct
22 Correct 40 ms 63852 KB Output is correct
23 Correct 40 ms 63872 KB Output is correct
24 Correct 41 ms 63800 KB Output is correct
25 Correct 47 ms 63852 KB Output is correct
26 Correct 40 ms 63852 KB Output is correct
27 Correct 42 ms 63980 KB Output is correct
28 Correct 40 ms 63852 KB Output is correct
29 Correct 41 ms 63852 KB Output is correct
30 Correct 41 ms 63872 KB Output is correct
31 Correct 40 ms 63852 KB Output is correct
32 Correct 40 ms 63724 KB Output is correct
33 Correct 40 ms 63852 KB Output is correct
34 Correct 42 ms 63980 KB Output is correct
35 Correct 130 ms 69868 KB Output is correct
36 Correct 203 ms 73964 KB Output is correct
37 Correct 172 ms 74092 KB Output is correct
38 Correct 167 ms 73452 KB Output is correct
39 Correct 43 ms 64108 KB Output is correct
40 Correct 44 ms 64364 KB Output is correct
41 Correct 44 ms 64236 KB Output is correct
42 Correct 42 ms 64068 KB Output is correct
43 Correct 43 ms 64236 KB Output is correct
44 Correct 43 ms 64236 KB Output is correct
45 Correct 43 ms 64108 KB Output is correct
46 Correct 43 ms 64108 KB Output is correct
47 Correct 43 ms 64236 KB Output is correct
48 Correct 44 ms 64236 KB Output is correct
49 Correct 59 ms 65644 KB Output is correct
50 Correct 184 ms 74348 KB Output is correct
51 Correct 50 ms 64748 KB Output is correct
52 Correct 156 ms 71404 KB Output is correct
53 Correct 53 ms 65516 KB Output is correct
54 Correct 170 ms 72904 KB Output is correct
55 Correct 48 ms 65004 KB Output is correct
56 Correct 47 ms 65004 KB Output is correct
57 Correct 48 ms 65004 KB Output is correct
58 Correct 48 ms 65004 KB Output is correct
59 Correct 42 ms 64108 KB Output is correct
60 Correct 125 ms 67820 KB Output is correct
61 Correct 45 ms 64364 KB Output is correct
62 Correct 170 ms 73336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 63724 KB Output is correct
2 Correct 39 ms 63836 KB Output is correct
3 Correct 39 ms 63724 KB Output is correct
4 Correct 39 ms 63724 KB Output is correct
5 Correct 41 ms 63724 KB Output is correct
6 Correct 39 ms 63852 KB Output is correct
7 Correct 41 ms 63852 KB Output is correct
8 Correct 41 ms 63852 KB Output is correct
9 Correct 41 ms 63852 KB Output is correct
10 Correct 41 ms 63724 KB Output is correct
11 Correct 40 ms 63852 KB Output is correct
12 Correct 44 ms 63724 KB Output is correct
13 Correct 41 ms 63724 KB Output is correct
14 Correct 39 ms 63724 KB Output is correct
15 Correct 40 ms 63724 KB Output is correct
16 Correct 40 ms 63852 KB Output is correct
17 Correct 39 ms 63724 KB Output is correct
18 Correct 39 ms 63724 KB Output is correct
19 Correct 41 ms 63852 KB Output is correct
20 Correct 39 ms 63852 KB Output is correct
21 Correct 40 ms 63852 KB Output is correct
22 Correct 40 ms 63852 KB Output is correct
23 Correct 40 ms 63872 KB Output is correct
24 Correct 41 ms 63800 KB Output is correct
25 Correct 47 ms 63852 KB Output is correct
26 Correct 40 ms 63852 KB Output is correct
27 Correct 42 ms 63980 KB Output is correct
28 Correct 40 ms 63852 KB Output is correct
29 Correct 41 ms 63852 KB Output is correct
30 Correct 41 ms 63872 KB Output is correct
31 Correct 40 ms 63852 KB Output is correct
32 Correct 40 ms 63724 KB Output is correct
33 Correct 40 ms 63852 KB Output is correct
34 Correct 42 ms 63980 KB Output is correct
35 Correct 130 ms 69868 KB Output is correct
36 Correct 203 ms 73964 KB Output is correct
37 Correct 172 ms 74092 KB Output is correct
38 Correct 167 ms 73452 KB Output is correct
39 Correct 43 ms 64108 KB Output is correct
40 Correct 44 ms 64364 KB Output is correct
41 Correct 44 ms 64236 KB Output is correct
42 Correct 42 ms 64068 KB Output is correct
43 Correct 43 ms 64236 KB Output is correct
44 Correct 43 ms 64236 KB Output is correct
45 Correct 43 ms 64108 KB Output is correct
46 Correct 43 ms 64108 KB Output is correct
47 Correct 43 ms 64236 KB Output is correct
48 Correct 44 ms 64236 KB Output is correct
49 Correct 59 ms 65644 KB Output is correct
50 Correct 184 ms 74348 KB Output is correct
51 Correct 50 ms 64748 KB Output is correct
52 Correct 156 ms 71404 KB Output is correct
53 Correct 53 ms 65516 KB Output is correct
54 Correct 170 ms 72904 KB Output is correct
55 Correct 48 ms 65004 KB Output is correct
56 Correct 47 ms 65004 KB Output is correct
57 Correct 48 ms 65004 KB Output is correct
58 Correct 48 ms 65004 KB Output is correct
59 Correct 42 ms 64108 KB Output is correct
60 Correct 125 ms 67820 KB Output is correct
61 Correct 45 ms 64364 KB Output is correct
62 Correct 170 ms 73336 KB Output is correct
63 Correct 838 ms 144152 KB Output is correct
64 Correct 834 ms 143980 KB Output is correct
65 Correct 844 ms 144108 KB Output is correct
66 Correct 295 ms 78300 KB Output is correct
67 Correct 541 ms 87916 KB Output is correct
68 Correct 301 ms 78300 KB Output is correct
69 Correct 524 ms 84572 KB Output is correct
70 Correct 290 ms 78432 KB Output is correct
71 Correct 317 ms 78296 KB Output is correct
72 Correct 633 ms 87020 KB Output is correct
73 Correct 581 ms 87148 KB Output is correct
74 Correct 1561 ms 122160 KB Output is correct
75 Correct 810 ms 93404 KB Output is correct
76 Correct 1223 ms 109800 KB Output is correct
77 Correct 1257 ms 110188 KB Output is correct
78 Correct 343 ms 86492 KB Output is correct
79 Correct 660 ms 92148 KB Output is correct
80 Correct 346 ms 86768 KB Output is correct
81 Correct 470 ms 88412 KB Output is correct
82 Correct 1215 ms 121184 KB Output is correct
83 Correct 1236 ms 121352 KB Output is correct
84 Correct 1044 ms 121328 KB Output is correct
85 Correct 1028 ms 121312 KB Output is correct
86 Correct 252 ms 76908 KB Output is correct
87 Correct 280 ms 78060 KB Output is correct
88 Correct 615 ms 87272 KB Output is correct
89 Correct 1167 ms 107616 KB Output is correct