답안 #700728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
700728 2023-02-19T08:34:27 Z sunwukong123 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1112 ms 94888 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach 
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 100005;
int n,m;
int ans;
struct ufds_ {
	int p[MAXN], sz[MAXN];
	unordered_set<int> innodes[MAXN];
	unordered_set<int> in[MAXN], out[MAXN];
	ufds_() {
		iota(p,p+MAXN,0);
		fill(sz,sz+MAXN,1);
		FOR(i,0,MAXN-1) {
			innodes[i].insert(i);
		}
	}
	int find(int x) {
		if(x==p[x])return x;
		else return p[x]=find(p[x]);
	}
} ufds;
int pairs(int sz) {
	return sz*(sz-1);
}
int32_t main() 
{
	IAMSPEED
	cin >> n >> m;
	FOR(idx,1,m) {
		int a,b; cin >> a >> b;
		// insert a->b 
		int ga=ufds.find(a);
		int gb=ufds.find(b);
	
		if(ga==gb){
			cout<<ans<<'\n';
			continue;
		}
		
		if(ufds.in[ga].find(gb) != ufds.in[ga].end()) {
			
			queue<pi> q;
			q.push({ga,gb}); // merge ga, gb;
			while(q.size()) {
				pi cur=q.front(); q.pop();
				if(ufds.find(cur.f) == ufds.find(cur.s))continue;
				int x= ufds.find(cur.f), y=ufds.find(cur.s);


				ans-=pairs(ufds.sz[x]);
				ans-=ufds.sz[x] * (ufds.innodes[x].size() - ufds.sz[x]);

				ans-=pairs(ufds.sz[y]);
				ans-=ufds.sz[y] * (ufds.innodes[y].size() - ufds.sz[y]);

				if(ufds.innodes[x].size() < ufds.innodes[y].size())swap(x,y);
				// innodes[x].size() >= innodes[y].size()
				for(auto i:ufds.innodes[y])ufds.innodes[x].insert(i);
				ufds.sz[x]+=ufds.sz[y];
				ufds.p[y] = x;
			
				ans += pairs(ufds.sz[x]);
				
				ans += ufds.sz[x] * (ufds.innodes[x].size() - ufds.sz[x]);
				
				for(auto i:ufds.out[y]) { // these things need to be updated from y to x.
					ufds.in[i].erase(y);
					ufds.in[i].insert(x);
					ufds.out[x].insert(i);
					if(ufds.in[x].find(i) != ufds.in[x].end()) {
						q.push({x,i});
					}

				}
				for(auto i:ufds.in[y]){
					ufds.in[x].insert(i);
					ufds.out[i].erase(y);
					ufds.out[i].insert(x);
					if(ufds.in[i].find(x) != ufds.in[i].end()) {
						q.push({x,i});
					}
				}


			}
			
		} else {
			if(ufds.innodes[gb].find(a) == ufds.innodes[gb].end()) {
				ufds.in[gb].insert(ga);
				ufds.innodes[gb].insert(a);
				ufds.out[ga].insert(gb);

				ans+=ufds.sz[gb];
			}
			
		}
		cout << ans << '\n';
		
	}

	return (0-0) ; 
}



# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 32440 KB Output is correct
2 Correct 30 ms 32404 KB Output is correct
3 Correct 38 ms 32360 KB Output is correct
4 Correct 32 ms 32308 KB Output is correct
5 Correct 26 ms 32328 KB Output is correct
6 Correct 35 ms 32292 KB Output is correct
7 Correct 33 ms 32468 KB Output is correct
8 Correct 37 ms 32424 KB Output is correct
9 Correct 25 ms 32468 KB Output is correct
10 Correct 27 ms 32328 KB Output is correct
11 Correct 33 ms 32416 KB Output is correct
12 Correct 26 ms 32312 KB Output is correct
13 Correct 26 ms 32356 KB Output is correct
14 Correct 35 ms 32332 KB Output is correct
15 Correct 25 ms 32400 KB Output is correct
16 Correct 30 ms 32456 KB Output is correct
17 Correct 25 ms 32412 KB Output is correct
18 Correct 24 ms 32340 KB Output is correct
19 Correct 30 ms 32320 KB Output is correct
20 Correct 26 ms 32424 KB Output is correct
21 Correct 31 ms 32376 KB Output is correct
22 Correct 29 ms 32464 KB Output is correct
23 Correct 24 ms 32468 KB Output is correct
24 Correct 30 ms 32468 KB Output is correct
25 Correct 25 ms 32468 KB Output is correct
26 Correct 28 ms 32536 KB Output is correct
27 Correct 34 ms 32440 KB Output is correct
28 Correct 36 ms 32428 KB Output is correct
29 Correct 31 ms 32400 KB Output is correct
30 Correct 37 ms 32492 KB Output is correct
31 Correct 37 ms 32448 KB Output is correct
32 Correct 29 ms 32408 KB Output is correct
33 Correct 35 ms 32408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 32440 KB Output is correct
2 Correct 30 ms 32404 KB Output is correct
3 Correct 38 ms 32360 KB Output is correct
4 Correct 32 ms 32308 KB Output is correct
5 Correct 26 ms 32328 KB Output is correct
6 Correct 35 ms 32292 KB Output is correct
7 Correct 33 ms 32468 KB Output is correct
8 Correct 37 ms 32424 KB Output is correct
9 Correct 25 ms 32468 KB Output is correct
10 Correct 27 ms 32328 KB Output is correct
11 Correct 33 ms 32416 KB Output is correct
12 Correct 26 ms 32312 KB Output is correct
13 Correct 26 ms 32356 KB Output is correct
14 Correct 35 ms 32332 KB Output is correct
15 Correct 25 ms 32400 KB Output is correct
16 Correct 30 ms 32456 KB Output is correct
17 Correct 25 ms 32412 KB Output is correct
18 Correct 24 ms 32340 KB Output is correct
19 Correct 30 ms 32320 KB Output is correct
20 Correct 26 ms 32424 KB Output is correct
21 Correct 31 ms 32376 KB Output is correct
22 Correct 29 ms 32464 KB Output is correct
23 Correct 24 ms 32468 KB Output is correct
24 Correct 30 ms 32468 KB Output is correct
25 Correct 25 ms 32468 KB Output is correct
26 Correct 28 ms 32536 KB Output is correct
27 Correct 34 ms 32440 KB Output is correct
28 Correct 36 ms 32428 KB Output is correct
29 Correct 31 ms 32400 KB Output is correct
30 Correct 37 ms 32492 KB Output is correct
31 Correct 37 ms 32448 KB Output is correct
32 Correct 29 ms 32408 KB Output is correct
33 Correct 35 ms 32408 KB Output is correct
34 Correct 37 ms 32480 KB Output is correct
35 Correct 138 ms 38088 KB Output is correct
36 Correct 131 ms 40796 KB Output is correct
37 Correct 163 ms 40780 KB Output is correct
38 Correct 134 ms 40392 KB Output is correct
39 Correct 50 ms 32836 KB Output is correct
40 Correct 29 ms 33264 KB Output is correct
41 Correct 39 ms 33332 KB Output is correct
42 Correct 29 ms 32884 KB Output is correct
43 Correct 36 ms 32880 KB Output is correct
44 Correct 38 ms 32772 KB Output is correct
45 Correct 45 ms 32844 KB Output is correct
46 Correct 33 ms 32876 KB Output is correct
47 Correct 28 ms 33100 KB Output is correct
48 Correct 41 ms 33096 KB Output is correct
49 Correct 32 ms 33712 KB Output is correct
50 Correct 153 ms 40936 KB Output is correct
51 Correct 41 ms 33364 KB Output is correct
52 Correct 121 ms 39332 KB Output is correct
53 Correct 43 ms 33720 KB Output is correct
54 Correct 145 ms 40076 KB Output is correct
55 Correct 52 ms 33316 KB Output is correct
56 Correct 31 ms 33320 KB Output is correct
57 Correct 29 ms 33376 KB Output is correct
58 Correct 34 ms 33392 KB Output is correct
59 Correct 33 ms 33316 KB Output is correct
60 Correct 163 ms 38240 KB Output is correct
61 Correct 41 ms 33180 KB Output is correct
62 Correct 126 ms 40312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 32440 KB Output is correct
2 Correct 30 ms 32404 KB Output is correct
3 Correct 38 ms 32360 KB Output is correct
4 Correct 32 ms 32308 KB Output is correct
5 Correct 26 ms 32328 KB Output is correct
6 Correct 35 ms 32292 KB Output is correct
7 Correct 33 ms 32468 KB Output is correct
8 Correct 37 ms 32424 KB Output is correct
9 Correct 25 ms 32468 KB Output is correct
10 Correct 27 ms 32328 KB Output is correct
11 Correct 33 ms 32416 KB Output is correct
12 Correct 26 ms 32312 KB Output is correct
13 Correct 26 ms 32356 KB Output is correct
14 Correct 35 ms 32332 KB Output is correct
15 Correct 25 ms 32400 KB Output is correct
16 Correct 30 ms 32456 KB Output is correct
17 Correct 25 ms 32412 KB Output is correct
18 Correct 24 ms 32340 KB Output is correct
19 Correct 30 ms 32320 KB Output is correct
20 Correct 26 ms 32424 KB Output is correct
21 Correct 31 ms 32376 KB Output is correct
22 Correct 29 ms 32464 KB Output is correct
23 Correct 24 ms 32468 KB Output is correct
24 Correct 30 ms 32468 KB Output is correct
25 Correct 25 ms 32468 KB Output is correct
26 Correct 28 ms 32536 KB Output is correct
27 Correct 34 ms 32440 KB Output is correct
28 Correct 36 ms 32428 KB Output is correct
29 Correct 31 ms 32400 KB Output is correct
30 Correct 37 ms 32492 KB Output is correct
31 Correct 37 ms 32448 KB Output is correct
32 Correct 29 ms 32408 KB Output is correct
33 Correct 35 ms 32408 KB Output is correct
34 Correct 37 ms 32480 KB Output is correct
35 Correct 138 ms 38088 KB Output is correct
36 Correct 131 ms 40796 KB Output is correct
37 Correct 163 ms 40780 KB Output is correct
38 Correct 134 ms 40392 KB Output is correct
39 Correct 50 ms 32836 KB Output is correct
40 Correct 29 ms 33264 KB Output is correct
41 Correct 39 ms 33332 KB Output is correct
42 Correct 29 ms 32884 KB Output is correct
43 Correct 36 ms 32880 KB Output is correct
44 Correct 38 ms 32772 KB Output is correct
45 Correct 45 ms 32844 KB Output is correct
46 Correct 33 ms 32876 KB Output is correct
47 Correct 28 ms 33100 KB Output is correct
48 Correct 41 ms 33096 KB Output is correct
49 Correct 32 ms 33712 KB Output is correct
50 Correct 153 ms 40936 KB Output is correct
51 Correct 41 ms 33364 KB Output is correct
52 Correct 121 ms 39332 KB Output is correct
53 Correct 43 ms 33720 KB Output is correct
54 Correct 145 ms 40076 KB Output is correct
55 Correct 52 ms 33316 KB Output is correct
56 Correct 31 ms 33320 KB Output is correct
57 Correct 29 ms 33376 KB Output is correct
58 Correct 34 ms 33392 KB Output is correct
59 Correct 33 ms 33316 KB Output is correct
60 Correct 163 ms 38240 KB Output is correct
61 Correct 41 ms 33180 KB Output is correct
62 Correct 126 ms 40312 KB Output is correct
63 Correct 555 ms 86804 KB Output is correct
64 Correct 560 ms 86832 KB Output is correct
65 Correct 523 ms 86944 KB Output is correct
66 Correct 233 ms 58432 KB Output is correct
67 Correct 629 ms 88000 KB Output is correct
68 Correct 244 ms 58468 KB Output is correct
69 Correct 369 ms 57764 KB Output is correct
70 Correct 239 ms 58440 KB Output is correct
71 Correct 244 ms 58552 KB Output is correct
72 Correct 513 ms 71892 KB Output is correct
73 Correct 562 ms 73368 KB Output is correct
74 Correct 1112 ms 94888 KB Output is correct
75 Correct 493 ms 67228 KB Output is correct
76 Correct 694 ms 80256 KB Output is correct
77 Correct 731 ms 80336 KB Output is correct
78 Correct 229 ms 64556 KB Output is correct
79 Correct 371 ms 72392 KB Output is correct
80 Correct 198 ms 61712 KB Output is correct
81 Correct 361 ms 66692 KB Output is correct
82 Correct 673 ms 83816 KB Output is correct
83 Correct 601 ms 83676 KB Output is correct
84 Correct 545 ms 82352 KB Output is correct
85 Correct 534 ms 82452 KB Output is correct
86 Correct 467 ms 92104 KB Output is correct
87 Correct 583 ms 94108 KB Output is correct
88 Correct 582 ms 73052 KB Output is correct
89 Correct 670 ms 79240 KB Output is correct