답안 #982423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982423 2024-05-14T08:37:54 Z Lib 자매 도시 (APIO20_swap) C++14
100 / 100
370 ms 71600 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "O2")
using namespace std;
int n,m,a,b,c;
vector <vector <int> > InComponents;
vector <vector <int> > adj; //the edge list of the KRT
vector <int> TVector;
int RootID; //ID of the root node of the KRT;
struct Edge{
	long long Weight;
	int Start;
	int End;
};
bool operator< (const Edge &x, const Edge &y){
	return x.Weight<y.Weight;
}
Edge Elist[500003];
int Depth[500003];
int Deg[500003];
int CurRep[500003]; //id of the respresentative node on the KRT
int Par[500003]; //Parent of node with id i on the KRT;
int Toggle[500003]; //is the i-th node on the KRT toggled?
int Check[500003];
int Ancestor[500003][25];
int Val[500003];//the value of the i-th node of the KRT
vector <int> KRTID;
int GetRep(int u){
	if(CurRep[u]==-1){
		return u;
	}else{
		return CurRep[u]=GetRep(CurRep[u]);
	}
}
void DSU(int id, int u, int v, int w){
	int CurID=n-1+id;
	Deg[u]++;
	Deg[v]++;
	//u and v belongs to the same component. The component now has a cycle. Immediately toggle the root node of the current 
	//subtree that includes both u and v on the KRT
	//Screw partial, binary tree KRT, opt into full-KRT instead
	int RepU=GetRep(u), RepV=GetRep(v);
	if(RepU==RepV){
		Par[RepU]=CurID;
		Val[CurID]=w;
		adj[RepU].push_back(CurID);
		adj[CurID].push_back(RepU);
		CurRep[RepU]=CurID;
		//The node isn't toggled just yet. Toggle it immediately as there is a cycle
		Toggle[CurID]=1;
	}else{
		//u and v doesn't belong to the same component. Merge them
		int OldComp1=RepU, OldComp2=RepV;
		adj[RepU].push_back(CurID);
		adj[RepV].push_back(CurID);
		adj[CurID].push_back(RepU);
		adj[CurID].push_back(RepV);
		Par[RepU]=CurID;
		Par[RepV]=CurID;
		CurRep[RepU]=CurID;
		CurRep[RepV]=CurID;
		Val[CurID]=w; //if I got WA because I forgot to set the fucking node weight to the nodes on the KRT, I'll shove 3 fingers up my ass.
		//Actually 2 because 3 is too much
		//if either of the components are toggled already (having a cycle OR a vertex with degree >=3)
		//OR the merged component has a vertex with degree >3 (either u or v), toggle immediately
		if(Toggle[OldComp1]||Toggle[OldComp2]||Deg[u]>2||Deg[v]>2){
			Toggle[CurID]=1;
		}
	}
}
void init(int N, int M, vector <int> U, vector <int> V, vector <int> W){
	n=N;
	m=M;
	for(int i=0;i<n+m+5;i++){
		adj.push_back(TVector);
	}
	for(int i=1;i<=m;i++){
		Elist[i].Weight=W[i-1];
		Elist[i].Start=U[i-1];
		Elist[i].End=V[i-1];
	}
	sort(Elist+1,Elist+m+1);
	for(int i=0;i<=m+n+1;i++){
		CurRep[i]=-1;
		Par[i]=-1;
		Toggle[i]=0;
	}		
	for(int i=1;i<=m;i++){
		DSU(i,Elist[i].Start,Elist[i].End,Elist[i].Weight);
	}
	//The KRT is now built. BFS from the root node and initialize LCA or something
	deque <int> dq;
	RootID=n+m-1;
	dq.push_back(RootID);
	Check[RootID]=1;
	int Cur;
	while(!dq.empty()){
		Cur=dq.front();
		for(int i=0;i<adj[Cur].size();i++){
			if(!Check[adj[Cur][i]]){
				dq.push_back(adj[Cur][i]);
				Check[adj[Cur][i]]=1;
				Depth[adj[Cur][i]]=Depth[Cur]+1;
			}
		}
		dq.pop_front();
	}
	for(int i=0;i<=20;i++){
		for(int k=0;k<n+m;k++){
			if(i==0){
				Ancestor[k][0]=Par[k];
			}else{
				if(Depth[k]<pow(2,i)){
					Ancestor[k][i]=-1;
				}else{
				Ancestor[k][i]=Ancestor[Ancestor[k][i-1]][i-1];
				}
			}
		}
	}
}
int getMinimumFuelCapacity(int x, int y){
	//I thought that we actually need to use binary lifting to find LCA and then jump from the LCA to find the nearest
	//toggled node on the KRT
	//But apparently small-to-large merging ensures that the depth of the tree is always logN or something, so bruteforcing
	//it is
	//ok nvm this guy is a fucking moron lmao LogN depth my ass. Wrong analysis, wasted 5 minutes. Fuck me
	if(!Toggle[RootID]){
		return -1;
	}
	if(Depth[x]<Depth[y]){
		swap(x,y);
	}

	int JumpLevel=Depth[x]-Depth[y];
	for(int i=19;i>=0;i--){
		if(JumpLevel >> i & 1){
			x=Ancestor[x][i];
		}
	}
	for(int i=20;i>=0;i--){
		if(Ancestor[x][i]!=Ancestor[y][i]){
			x=Ancestor[x][i];
			y=Ancestor[y][i];
		}
	}
	x=Par[x];
	y=Par[y];
	if(Toggle[x]){
		return Val[x];
	}
	for(int i=20;i>=0;i--){
		if(Ancestor[x][i]!=-1&&!Toggle[Ancestor[x][i]]){
			x=Ancestor[x][i];
		}
	}
	x=Par[x];
	if(Toggle[x]&&x!=-1){
		return Val[x];
	}
}
/*
Post-AC comments:
- In a classic me fashion, did pretty much everything right, built the KRT which is relatively complicated bullshit (not really but well) and all, but then
......fucks up BINARY LIFTING. Yep, classic me.
- Use a Full-KRT instead of a Binary-KRT (i.e: when the DSU adds an edge that connects between 2 vertex that is currently in the same connected components,
instead of discarding that edge completely, still add it into the KRT instead). Less casework and headache - inducing bugs that way.
- This is literally the first time I've touched DSU since...May last year (that's why the first few submissions are utter fucking dogshit with some shitass bruteforce
union merging that doesn't even work), also the first time I implemented the KRT as well. Fun stuffs,
*/

//IO for later uses
/*
int q;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("swap_12_2.in","r",stdin);
	cin>>n>>m;
	vector <int> U,V,W;
	for(int i=1;i<=m;i++){
		cin>>a>>b>>c;
		U.push_back(a);
		V.push_back(b);
		W.push_back(c);
	}
	init(n,m,U,V,W);
	deque <int> dq;
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		dq.push_back(getMinimumFuelCapacity(a,b));
		cout<<dq.back()<<"\n";
	}
	freopen("swap_12_2.out","r",stdin);
	int cur, score=0;
	for(int i=1;i<=q;i++){
		cin>>cur;
		if(cur==dq.front()){
			score++;
		}else{
			cout<<dq.front()<<" "<<cur<<" "<<Val[RootID]<<"\n";
		}
		dq.pop_front();
	}
	cout<<score<<" "<<q<<" ";
	int maxdepth=0;
	for(int i=0;i<=n+m+5;i++){
		maxdepth=max(maxdepth,Depth[i]);
	}
	//cout<<maxdepth;
}
*/

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:98:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for(int i=0;i<adj[Cur].size();i++){
      |               ~^~~~~~~~~~~~~~~~
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:160:1: warning: control reaches end of non-void function [-Wreturn-type]
  160 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 3 ms 15192 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 119 ms 43968 KB Output is correct
10 Correct 150 ms 49516 KB Output is correct
11 Correct 143 ms 47296 KB Output is correct
12 Correct 157 ms 51128 KB Output is correct
13 Correct 167 ms 51368 KB Output is correct
14 Correct 120 ms 44484 KB Output is correct
15 Correct 189 ms 51776 KB Output is correct
16 Correct 187 ms 50296 KB Output is correct
17 Correct 205 ms 52416 KB Output is correct
18 Correct 215 ms 52336 KB Output is correct
19 Correct 79 ms 24520 KB Output is correct
20 Correct 240 ms 54024 KB Output is correct
21 Correct 234 ms 51828 KB Output is correct
22 Correct 258 ms 53696 KB Output is correct
23 Correct 303 ms 54040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 272 ms 51556 KB Output is correct
4 Correct 260 ms 52760 KB Output is correct
5 Correct 289 ms 52560 KB Output is correct
6 Correct 263 ms 53160 KB Output is correct
7 Correct 266 ms 52644 KB Output is correct
8 Correct 245 ms 50284 KB Output is correct
9 Correct 257 ms 53804 KB Output is correct
10 Correct 293 ms 51612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 3 ms 15192 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 2 ms 14680 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14972 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 4 ms 14684 KB Output is correct
24 Correct 5 ms 16984 KB Output is correct
25 Correct 4 ms 16988 KB Output is correct
26 Correct 4 ms 17108 KB Output is correct
27 Correct 5 ms 14940 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 3 ms 15192 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 119 ms 43968 KB Output is correct
11 Correct 150 ms 49516 KB Output is correct
12 Correct 143 ms 47296 KB Output is correct
13 Correct 157 ms 51128 KB Output is correct
14 Correct 167 ms 51368 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14972 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 4 ms 14684 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 4 ms 14940 KB Output is correct
28 Correct 4 ms 14684 KB Output is correct
29 Correct 5 ms 16984 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 4 ms 17108 KB Output is correct
32 Correct 5 ms 14940 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
34 Correct 21 ms 20692 KB Output is correct
35 Correct 165 ms 50104 KB Output is correct
36 Correct 161 ms 51384 KB Output is correct
37 Correct 176 ms 50372 KB Output is correct
38 Correct 154 ms 50868 KB Output is correct
39 Correct 155 ms 50872 KB Output is correct
40 Correct 142 ms 48384 KB Output is correct
41 Correct 155 ms 51148 KB Output is correct
42 Correct 185 ms 50092 KB Output is correct
43 Correct 207 ms 49852 KB Output is correct
44 Correct 184 ms 50392 KB Output is correct
45 Correct 205 ms 54732 KB Output is correct
46 Correct 178 ms 49848 KB Output is correct
47 Correct 159 ms 49852 KB Output is correct
48 Correct 170 ms 50812 KB Output is correct
49 Correct 143 ms 46772 KB Output is correct
50 Correct 106 ms 39452 KB Output is correct
51 Correct 169 ms 51380 KB Output is correct
52 Correct 216 ms 61112 KB Output is correct
53 Correct 247 ms 65968 KB Output is correct
54 Correct 281 ms 70548 KB Output is correct
55 Correct 164 ms 51140 KB Output is correct
56 Correct 248 ms 64176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 3 ms 15192 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 119 ms 43968 KB Output is correct
10 Correct 150 ms 49516 KB Output is correct
11 Correct 143 ms 47296 KB Output is correct
12 Correct 157 ms 51128 KB Output is correct
13 Correct 167 ms 51368 KB Output is correct
14 Correct 120 ms 44484 KB Output is correct
15 Correct 189 ms 51776 KB Output is correct
16 Correct 187 ms 50296 KB Output is correct
17 Correct 205 ms 52416 KB Output is correct
18 Correct 215 ms 52336 KB Output is correct
19 Correct 272 ms 51556 KB Output is correct
20 Correct 260 ms 52760 KB Output is correct
21 Correct 289 ms 52560 KB Output is correct
22 Correct 263 ms 53160 KB Output is correct
23 Correct 266 ms 52644 KB Output is correct
24 Correct 245 ms 50284 KB Output is correct
25 Correct 257 ms 53804 KB Output is correct
26 Correct 293 ms 51612 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 14972 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 4 ms 14684 KB Output is correct
31 Correct 3 ms 14940 KB Output is correct
32 Correct 4 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 3 ms 14940 KB Output is correct
35 Correct 3 ms 14940 KB Output is correct
36 Correct 21 ms 20692 KB Output is correct
37 Correct 165 ms 50104 KB Output is correct
38 Correct 161 ms 51384 KB Output is correct
39 Correct 176 ms 50372 KB Output is correct
40 Correct 154 ms 50868 KB Output is correct
41 Correct 155 ms 50872 KB Output is correct
42 Correct 142 ms 48384 KB Output is correct
43 Correct 155 ms 51148 KB Output is correct
44 Correct 185 ms 50092 KB Output is correct
45 Correct 207 ms 49852 KB Output is correct
46 Correct 184 ms 50392 KB Output is correct
47 Correct 26 ms 20944 KB Output is correct
48 Correct 244 ms 52900 KB Output is correct
49 Correct 240 ms 52792 KB Output is correct
50 Correct 237 ms 53040 KB Output is correct
51 Correct 241 ms 52524 KB Output is correct
52 Correct 223 ms 50280 KB Output is correct
53 Correct 175 ms 43692 KB Output is correct
54 Correct 248 ms 53540 KB Output is correct
55 Correct 239 ms 53204 KB Output is correct
56 Correct 283 ms 54040 KB Output is correct
57 Correct 244 ms 54348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 3 ms 15192 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 119 ms 43968 KB Output is correct
11 Correct 150 ms 49516 KB Output is correct
12 Correct 143 ms 47296 KB Output is correct
13 Correct 157 ms 51128 KB Output is correct
14 Correct 167 ms 51368 KB Output is correct
15 Correct 120 ms 44484 KB Output is correct
16 Correct 189 ms 51776 KB Output is correct
17 Correct 187 ms 50296 KB Output is correct
18 Correct 205 ms 52416 KB Output is correct
19 Correct 215 ms 52336 KB Output is correct
20 Correct 272 ms 51556 KB Output is correct
21 Correct 260 ms 52760 KB Output is correct
22 Correct 289 ms 52560 KB Output is correct
23 Correct 263 ms 53160 KB Output is correct
24 Correct 266 ms 52644 KB Output is correct
25 Correct 245 ms 50284 KB Output is correct
26 Correct 257 ms 53804 KB Output is correct
27 Correct 293 ms 51612 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14972 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 ms 14684 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 4 ms 14940 KB Output is correct
34 Correct 3 ms 14940 KB Output is correct
35 Correct 3 ms 14940 KB Output is correct
36 Correct 3 ms 14940 KB Output is correct
37 Correct 21 ms 20692 KB Output is correct
38 Correct 165 ms 50104 KB Output is correct
39 Correct 161 ms 51384 KB Output is correct
40 Correct 176 ms 50372 KB Output is correct
41 Correct 154 ms 50868 KB Output is correct
42 Correct 155 ms 50872 KB Output is correct
43 Correct 142 ms 48384 KB Output is correct
44 Correct 155 ms 51148 KB Output is correct
45 Correct 185 ms 50092 KB Output is correct
46 Correct 207 ms 49852 KB Output is correct
47 Correct 184 ms 50392 KB Output is correct
48 Correct 26 ms 20944 KB Output is correct
49 Correct 244 ms 52900 KB Output is correct
50 Correct 240 ms 52792 KB Output is correct
51 Correct 237 ms 53040 KB Output is correct
52 Correct 241 ms 52524 KB Output is correct
53 Correct 223 ms 50280 KB Output is correct
54 Correct 175 ms 43692 KB Output is correct
55 Correct 248 ms 53540 KB Output is correct
56 Correct 239 ms 53204 KB Output is correct
57 Correct 283 ms 54040 KB Output is correct
58 Correct 244 ms 54348 KB Output is correct
59 Correct 79 ms 24520 KB Output is correct
60 Correct 240 ms 54024 KB Output is correct
61 Correct 234 ms 51828 KB Output is correct
62 Correct 258 ms 53696 KB Output is correct
63 Correct 303 ms 54040 KB Output is correct
64 Correct 3 ms 14936 KB Output is correct
65 Correct 4 ms 14940 KB Output is correct
66 Correct 3 ms 14940 KB Output is correct
67 Correct 4 ms 14940 KB Output is correct
68 Correct 4 ms 14684 KB Output is correct
69 Correct 5 ms 16984 KB Output is correct
70 Correct 4 ms 16988 KB Output is correct
71 Correct 4 ms 17108 KB Output is correct
72 Correct 5 ms 14940 KB Output is correct
73 Correct 4 ms 16988 KB Output is correct
74 Correct 205 ms 54732 KB Output is correct
75 Correct 178 ms 49848 KB Output is correct
76 Correct 159 ms 49852 KB Output is correct
77 Correct 170 ms 50812 KB Output is correct
78 Correct 143 ms 46772 KB Output is correct
79 Correct 106 ms 39452 KB Output is correct
80 Correct 169 ms 51380 KB Output is correct
81 Correct 216 ms 61112 KB Output is correct
82 Correct 247 ms 65968 KB Output is correct
83 Correct 281 ms 70548 KB Output is correct
84 Correct 164 ms 51140 KB Output is correct
85 Correct 248 ms 64176 KB Output is correct
86 Correct 86 ms 30444 KB Output is correct
87 Correct 229 ms 52508 KB Output is correct
88 Correct 225 ms 52468 KB Output is correct
89 Correct 250 ms 53328 KB Output is correct
90 Correct 205 ms 49680 KB Output is correct
91 Correct 255 ms 49188 KB Output is correct
92 Correct 253 ms 50708 KB Output is correct
93 Correct 360 ms 63348 KB Output is correct
94 Correct 370 ms 69188 KB Output is correct
95 Correct 346 ms 71600 KB Output is correct
96 Correct 278 ms 52504 KB Output is correct
97 Correct 316 ms 59712 KB Output is correct