답안 #313141

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
313141 2020-10-15T09:09:06 Z vipghn2003 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
17 / 100
5000 ms 44408 KB
#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;

const int N=1e5+5;
int n,m,p[N],sz[N];
set<int>in[N];
set<pii>out[N];
long long res;

int Find(int x)
{
	if(x==p[x]) return x;
	return x=Find(p[x]);
}

void Union(int u, int v)
{
	int x=Find(u);
	int y=Find(v);
	if(x==y) return;
	auto it=out[y].lower_bound(mp(x,0));
	if(it!=out[y].end()&&(*it).fi==x)
    {
		if(in[x].size()+out[x].size()<in[y].size()+out[y].size()) swap(x,y);
		vector<pii>vo;
		for(auto&i:out[y])
		{
			res-=sz[i.fi];
			in[i.fi].erase(i.se);
			if(i.fi!=x) vo.push_back(i);
		}
		out[y].clear();
		res+=1ll*sz[y]*(2*sz[x]+in[x].size()-in[y].size());
		vector<int>vi;
		for(auto&i:in[y])
		{
			int j=Find(i);
			out[j].erase(mp(y,i));
			if(j!=x) vi.push_back(i);
		}
		in[y].clear();
		p[y]=x;
        sz[x]+=sz[y];
		for(auto&i:vi) Union(i,x);
		for(auto&i:vo) Union(i.se,i.fi);
	}
	else if(!in[y].count(u))
	{
		res+=sz[y];
		in[y].insert(u);
		out[x].insert(mp(y,u));
	}
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        p[i]=i;
        sz[i]=1;
    }
    while(m--)
    {
        int u,v;
        cin>>u>>v;
        Union(u,v);
        cout<<res<<'\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 8 ms 9728 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 8 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9856 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 7 ms 9728 KB Output is correct
24 Correct 7 ms 9728 KB Output is correct
25 Correct 7 ms 9728 KB Output is correct
26 Correct 7 ms 9728 KB Output is correct
27 Correct 7 ms 9728 KB Output is correct
28 Correct 8 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 7 ms 9728 KB Output is correct
31 Correct 8 ms 9728 KB Output is correct
32 Correct 7 ms 9728 KB Output is correct
33 Correct 7 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 8 ms 9728 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 8 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9856 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 7 ms 9728 KB Output is correct
24 Correct 7 ms 9728 KB Output is correct
25 Correct 7 ms 9728 KB Output is correct
26 Correct 7 ms 9728 KB Output is correct
27 Correct 7 ms 9728 KB Output is correct
28 Correct 8 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 7 ms 9728 KB Output is correct
31 Correct 8 ms 9728 KB Output is correct
32 Correct 7 ms 9728 KB Output is correct
33 Correct 7 ms 9728 KB Output is correct
34 Correct 9 ms 9856 KB Output is correct
35 Correct 102 ms 15156 KB Output is correct
36 Correct 112 ms 17272 KB Output is correct
37 Correct 108 ms 17272 KB Output is correct
38 Correct 106 ms 17016 KB Output is correct
39 Correct 9 ms 9856 KB Output is correct
40 Correct 9 ms 9856 KB Output is correct
41 Correct 9 ms 9856 KB Output is correct
42 Correct 19 ms 9856 KB Output is correct
43 Correct 9 ms 9984 KB Output is correct
44 Correct 9 ms 9984 KB Output is correct
45 Correct 13 ms 9888 KB Output is correct
46 Correct 14 ms 9856 KB Output is correct
47 Correct 9 ms 9984 KB Output is correct
48 Correct 10 ms 9976 KB Output is correct
49 Correct 15 ms 10496 KB Output is correct
50 Correct 110 ms 17272 KB Output is correct
51 Correct 13 ms 10112 KB Output is correct
52 Correct 100 ms 16248 KB Output is correct
53 Correct 15 ms 10368 KB Output is correct
54 Correct 106 ms 16760 KB Output is correct
55 Correct 11 ms 10240 KB Output is correct
56 Correct 11 ms 10240 KB Output is correct
57 Correct 12 ms 10240 KB Output is correct
58 Correct 11 ms 10240 KB Output is correct
59 Correct 9 ms 9856 KB Output is correct
60 Correct 104 ms 14784 KB Output is correct
61 Correct 11 ms 9984 KB Output is correct
62 Correct 106 ms 16888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 8 ms 9728 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 8 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9856 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 7 ms 9728 KB Output is correct
24 Correct 7 ms 9728 KB Output is correct
25 Correct 7 ms 9728 KB Output is correct
26 Correct 7 ms 9728 KB Output is correct
27 Correct 7 ms 9728 KB Output is correct
28 Correct 8 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 7 ms 9728 KB Output is correct
31 Correct 8 ms 9728 KB Output is correct
32 Correct 7 ms 9728 KB Output is correct
33 Correct 7 ms 9728 KB Output is correct
34 Correct 9 ms 9856 KB Output is correct
35 Correct 102 ms 15156 KB Output is correct
36 Correct 112 ms 17272 KB Output is correct
37 Correct 108 ms 17272 KB Output is correct
38 Correct 106 ms 17016 KB Output is correct
39 Correct 9 ms 9856 KB Output is correct
40 Correct 9 ms 9856 KB Output is correct
41 Correct 9 ms 9856 KB Output is correct
42 Correct 19 ms 9856 KB Output is correct
43 Correct 9 ms 9984 KB Output is correct
44 Correct 9 ms 9984 KB Output is correct
45 Correct 13 ms 9888 KB Output is correct
46 Correct 14 ms 9856 KB Output is correct
47 Correct 9 ms 9984 KB Output is correct
48 Correct 10 ms 9976 KB Output is correct
49 Correct 15 ms 10496 KB Output is correct
50 Correct 110 ms 17272 KB Output is correct
51 Correct 13 ms 10112 KB Output is correct
52 Correct 100 ms 16248 KB Output is correct
53 Correct 15 ms 10368 KB Output is correct
54 Correct 106 ms 16760 KB Output is correct
55 Correct 11 ms 10240 KB Output is correct
56 Correct 11 ms 10240 KB Output is correct
57 Correct 12 ms 10240 KB Output is correct
58 Correct 11 ms 10240 KB Output is correct
59 Correct 9 ms 9856 KB Output is correct
60 Correct 104 ms 14784 KB Output is correct
61 Correct 11 ms 9984 KB Output is correct
62 Correct 106 ms 16888 KB Output is correct
63 Correct 418 ms 44292 KB Output is correct
64 Correct 421 ms 44156 KB Output is correct
65 Correct 417 ms 44408 KB Output is correct
66 Correct 100 ms 14968 KB Output is correct
67 Correct 188 ms 18936 KB Output is correct
68 Execution timed out 5044 ms 11992 KB Time limit exceeded
69 Halted 0 ms 0 KB -