답안 #236010

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236010 2020-05-30T18:58:53 Z rajarshi_basu 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
2216 ms 270000 KB
#include <stdio.h>     
#include <stdlib.h>    
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <deque>
#include <iomanip>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>

#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long 
#define ld long double
//#define int short
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define iii pair<int,ii>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define mp make_pair
#define vv vector
#define endl '\n'

using namespace std;
const int MAXN = 1e5+5;
int D = 0;
int E = 0;
int F = 0;
int G = 0;
struct DSU{
	ll tot = 0;
	int parent[MAXN];
	set<int> allINC[MAXN];
	set<int> allOUT[MAXN];
	map<int,set<int>/*set of all a, a->b*/ > incoming[MAXN];// individual nodes 
	map<int,set<int>/*set of all a, a->b*/ > outgoing[MAXN];
	
	set<int> allNodes[MAXN];
	DSU(){
		FOR(i,MAXN)parent[i] = i;
		FOR(i,MAXN)allNodes[i].insert(i);
	}
	int find(int a){
		if(a != parent[a])parent[a] = find(parent[a]);
		return parent[a];
	}
	bool isSame(int a,int b){
		return find(a) == find(b);
	}
	// outgoing[e] = {a,i} means there are i edges going from Supernodes e to a
	// we only keep track of outgoing edges + internal clique edges;
	

	inline void addEdge(int a,int b){
		// pa -> pb
		int pa = find(a);
		int pb = find(b);
		if(pa == pb)return;
		if(incoming[pb][pa].find(a) == incoming[pb][pa].end()){
			//cout << pa << " <--> " << pb << endl;
			allINC[pb].insert(a);
			allOUT[pa].insert(a);
			incoming[pb][pa].insert(a);
			outgoing[pa][pb].insert(a);
			tot += allNodes[pb].size();
			handleEdge(a,b);
		}
	}

	



	void merge(int a,int b){
		//cout << "MERGE: " << a << " " << b << endl;
		int pa = find(a);
		int pb = find(b);
		tot += 2*allNodes[pa].size()*allNodes[pb].size(); // new internal edges of the clique.
		if(allNodes[pa].size() < allNodes[pb].size())swap(pa,pb);
		//cout << pa << " " << pb << endl;

		tot -= incoming[pa][pb].size()*allNodes[pa].size();
		tot -= incoming[pb][pa].size()*allNodes[pb].size();
		//cout << "TOT HERE: " << tot << endl;

		// we are going to make pa the parent of pb;
		// lets iterate over everything of pb
		vi all;
		for(auto e: outgoing[pb]){
			if(find(e.ff) == pa)continue;
			if(find(e.ff) != e.ff)continue;
			all.pb(e.ff);
			for(auto x: e.ss)outgoing[pa][e.ff].insert(x);
			for(auto x: incoming[e.ff][pb]){
				allINC[e.ff].insert(x);
				incoming[e.ff][pa].insert(x);
			}
		}
		
		for(auto x: incoming[pa][pb])allINC[pa].erase(x);

		int cnt = allINC[pa].size();

		for(auto e: incoming[pb]){
			if(find(e.ff) == pa)continue;
			if(find(e.ff) != e.ff)continue;
			all.pb(e.ff);
			for(auto x: e.ss){
				// means x->pb exists; also x lies in component of e.ff
				// check if x->pa exists or not
				
				if(incoming[pa][e.ff].find(x) != incoming[pa][e.ff].end()){
					// means it is there:
					// then no need to do anything bleh
					cnt--;
				}else{
					// we need to add it there. 
					allINC[pa].insert(x);
					outgoing[e.ff][pa].insert(x);
					incoming[pa][e.ff].insert(x);
					
					tot += allNodes[pa].size();
				}
			}
		}
		
		tot += cnt*allNodes[pb].size();

		for(auto x : allNodes[pb])allNodes[pa].insert(x);
		parent[pb] = pa;
		for(auto e : all)handleEdge(e,pa);
	}


	bool check(int a,int b){
		int pa = find(a);
		int pb = find(b);
		return pa!=pb and !incoming[pa][pb].empty() and !incoming[pb][pa].empty();
	}

	void handleEdge(int a,int b){
		//cout <<a << " " << b << endl;
		if(isSame(a,b))return;
		
		if(!check(a,b))return;
		merge(a,b);
	}
};	

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int n,m;
	cin >> n >> m;
	DSU dsu;
	FOR(i,m){
		int a,b;
		cin >> a >> b;
		a--;b--;
		dsu.addEdge(a,b);
		cout << dsu.tot << endl;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 28928 KB Output is correct
2 Correct 25 ms 28928 KB Output is correct
3 Correct 24 ms 28928 KB Output is correct
4 Correct 24 ms 28928 KB Output is correct
5 Correct 25 ms 28928 KB Output is correct
6 Correct 24 ms 28928 KB Output is correct
7 Correct 28 ms 29184 KB Output is correct
8 Correct 25 ms 29056 KB Output is correct
9 Correct 26 ms 29056 KB Output is correct
10 Correct 25 ms 28928 KB Output is correct
11 Correct 25 ms 28928 KB Output is correct
12 Correct 24 ms 28928 KB Output is correct
13 Correct 24 ms 28928 KB Output is correct
14 Correct 24 ms 28928 KB Output is correct
15 Correct 25 ms 28928 KB Output is correct
16 Correct 25 ms 28928 KB Output is correct
17 Correct 24 ms 28928 KB Output is correct
18 Correct 25 ms 29056 KB Output is correct
19 Correct 24 ms 28928 KB Output is correct
20 Correct 24 ms 29056 KB Output is correct
21 Correct 27 ms 29184 KB Output is correct
22 Correct 24 ms 29056 KB Output is correct
23 Correct 25 ms 29056 KB Output is correct
24 Correct 24 ms 29056 KB Output is correct
25 Correct 25 ms 29056 KB Output is correct
26 Correct 25 ms 28928 KB Output is correct
27 Correct 25 ms 29056 KB Output is correct
28 Correct 25 ms 28928 KB Output is correct
29 Correct 26 ms 28928 KB Output is correct
30 Correct 25 ms 28928 KB Output is correct
31 Correct 27 ms 28928 KB Output is correct
32 Correct 24 ms 28928 KB Output is correct
33 Correct 25 ms 29056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 28928 KB Output is correct
2 Correct 25 ms 28928 KB Output is correct
3 Correct 24 ms 28928 KB Output is correct
4 Correct 24 ms 28928 KB Output is correct
5 Correct 25 ms 28928 KB Output is correct
6 Correct 24 ms 28928 KB Output is correct
7 Correct 28 ms 29184 KB Output is correct
8 Correct 25 ms 29056 KB Output is correct
9 Correct 26 ms 29056 KB Output is correct
10 Correct 25 ms 28928 KB Output is correct
11 Correct 25 ms 28928 KB Output is correct
12 Correct 24 ms 28928 KB Output is correct
13 Correct 24 ms 28928 KB Output is correct
14 Correct 24 ms 28928 KB Output is correct
15 Correct 25 ms 28928 KB Output is correct
16 Correct 25 ms 28928 KB Output is correct
17 Correct 24 ms 28928 KB Output is correct
18 Correct 25 ms 29056 KB Output is correct
19 Correct 24 ms 28928 KB Output is correct
20 Correct 24 ms 29056 KB Output is correct
21 Correct 27 ms 29184 KB Output is correct
22 Correct 24 ms 29056 KB Output is correct
23 Correct 25 ms 29056 KB Output is correct
24 Correct 24 ms 29056 KB Output is correct
25 Correct 25 ms 29056 KB Output is correct
26 Correct 25 ms 28928 KB Output is correct
27 Correct 25 ms 29056 KB Output is correct
28 Correct 25 ms 28928 KB Output is correct
29 Correct 26 ms 28928 KB Output is correct
30 Correct 25 ms 28928 KB Output is correct
31 Correct 27 ms 28928 KB Output is correct
32 Correct 24 ms 28928 KB Output is correct
33 Correct 25 ms 29056 KB Output is correct
34 Correct 29 ms 29312 KB Output is correct
35 Correct 130 ms 36472 KB Output is correct
36 Correct 180 ms 47480 KB Output is correct
37 Correct 180 ms 47612 KB Output is correct
38 Correct 171 ms 46328 KB Output is correct
39 Correct 32 ms 30456 KB Output is correct
40 Correct 34 ms 31356 KB Output is correct
41 Correct 42 ms 31352 KB Output is correct
42 Correct 31 ms 30336 KB Output is correct
43 Correct 31 ms 30456 KB Output is correct
44 Correct 32 ms 30336 KB Output is correct
45 Correct 33 ms 30464 KB Output is correct
46 Correct 32 ms 30464 KB Output is correct
47 Correct 34 ms 31104 KB Output is correct
48 Correct 35 ms 31224 KB Output is correct
49 Correct 51 ms 34304 KB Output is correct
50 Correct 196 ms 48120 KB Output is correct
51 Correct 38 ms 31484 KB Output is correct
52 Correct 144 ms 42104 KB Output is correct
53 Correct 48 ms 33784 KB Output is correct
54 Correct 161 ms 44872 KB Output is correct
55 Correct 37 ms 32000 KB Output is correct
56 Correct 37 ms 31992 KB Output is correct
57 Correct 37 ms 32640 KB Output is correct
58 Correct 37 ms 32760 KB Output is correct
59 Correct 32 ms 30840 KB Output is correct
60 Correct 108 ms 35960 KB Output is correct
61 Correct 37 ms 31480 KB Output is correct
62 Correct 168 ms 45816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 28928 KB Output is correct
2 Correct 25 ms 28928 KB Output is correct
3 Correct 24 ms 28928 KB Output is correct
4 Correct 24 ms 28928 KB Output is correct
5 Correct 25 ms 28928 KB Output is correct
6 Correct 24 ms 28928 KB Output is correct
7 Correct 28 ms 29184 KB Output is correct
8 Correct 25 ms 29056 KB Output is correct
9 Correct 26 ms 29056 KB Output is correct
10 Correct 25 ms 28928 KB Output is correct
11 Correct 25 ms 28928 KB Output is correct
12 Correct 24 ms 28928 KB Output is correct
13 Correct 24 ms 28928 KB Output is correct
14 Correct 24 ms 28928 KB Output is correct
15 Correct 25 ms 28928 KB Output is correct
16 Correct 25 ms 28928 KB Output is correct
17 Correct 24 ms 28928 KB Output is correct
18 Correct 25 ms 29056 KB Output is correct
19 Correct 24 ms 28928 KB Output is correct
20 Correct 24 ms 29056 KB Output is correct
21 Correct 27 ms 29184 KB Output is correct
22 Correct 24 ms 29056 KB Output is correct
23 Correct 25 ms 29056 KB Output is correct
24 Correct 24 ms 29056 KB Output is correct
25 Correct 25 ms 29056 KB Output is correct
26 Correct 25 ms 28928 KB Output is correct
27 Correct 25 ms 29056 KB Output is correct
28 Correct 25 ms 28928 KB Output is correct
29 Correct 26 ms 28928 KB Output is correct
30 Correct 25 ms 28928 KB Output is correct
31 Correct 27 ms 28928 KB Output is correct
32 Correct 24 ms 28928 KB Output is correct
33 Correct 25 ms 29056 KB Output is correct
34 Correct 29 ms 29312 KB Output is correct
35 Correct 130 ms 36472 KB Output is correct
36 Correct 180 ms 47480 KB Output is correct
37 Correct 180 ms 47612 KB Output is correct
38 Correct 171 ms 46328 KB Output is correct
39 Correct 32 ms 30456 KB Output is correct
40 Correct 34 ms 31356 KB Output is correct
41 Correct 42 ms 31352 KB Output is correct
42 Correct 31 ms 30336 KB Output is correct
43 Correct 31 ms 30456 KB Output is correct
44 Correct 32 ms 30336 KB Output is correct
45 Correct 33 ms 30464 KB Output is correct
46 Correct 32 ms 30464 KB Output is correct
47 Correct 34 ms 31104 KB Output is correct
48 Correct 35 ms 31224 KB Output is correct
49 Correct 51 ms 34304 KB Output is correct
50 Correct 196 ms 48120 KB Output is correct
51 Correct 38 ms 31484 KB Output is correct
52 Correct 144 ms 42104 KB Output is correct
53 Correct 48 ms 33784 KB Output is correct
54 Correct 161 ms 44872 KB Output is correct
55 Correct 37 ms 32000 KB Output is correct
56 Correct 37 ms 31992 KB Output is correct
57 Correct 37 ms 32640 KB Output is correct
58 Correct 37 ms 32760 KB Output is correct
59 Correct 32 ms 30840 KB Output is correct
60 Correct 108 ms 35960 KB Output is correct
61 Correct 37 ms 31480 KB Output is correct
62 Correct 168 ms 45816 KB Output is correct
63 Correct 898 ms 165840 KB Output is correct
64 Correct 902 ms 165768 KB Output is correct
65 Correct 893 ms 165684 KB Output is correct
66 Correct 691 ms 108536 KB Output is correct
67 Correct 948 ms 159352 KB Output is correct
68 Correct 630 ms 103896 KB Output is correct
69 Correct 960 ms 103928 KB Output is correct
70 Correct 697 ms 106232 KB Output is correct
71 Correct 706 ms 106204 KB Output is correct
72 Correct 1041 ms 144532 KB Output is correct
73 Correct 1044 ms 146780 KB Output is correct
74 Correct 2216 ms 270000 KB Output is correct
75 Correct 1308 ms 127840 KB Output is correct
76 Correct 1663 ms 196728 KB Output is correct
77 Correct 1662 ms 197288 KB Output is correct
78 Correct 525 ms 100472 KB Output is correct
79 Correct 923 ms 125352 KB Output is correct
80 Correct 578 ms 101292 KB Output is correct
81 Correct 975 ms 119592 KB Output is correct
82 Correct 1532 ms 183088 KB Output is correct
83 Correct 1499 ms 183140 KB Output is correct
84 Correct 1410 ms 219368 KB Output is correct
85 Correct 1427 ms 219228 KB Output is correct
86 Correct 646 ms 141304 KB Output is correct
87 Correct 654 ms 143684 KB Output is correct
88 Correct 1049 ms 147524 KB Output is correct
89 Correct 1647 ms 189280 KB Output is correct