답안 #981209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
981209 2024-05-13T02:21:46 Z Lib 자매 도시 (APIO20_swap) C++14
7 / 100
220 ms 64136 KB
#include <bits/stdc++.h>
using namespace std;
int n,m;
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][20];
long long Val[500003];//the value of the i-th node of the KRT
vector <int> KRTID;
void DSU(int id, int u, int v, int w){
	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
	if(CurRep[u]==CurRep[v]){
		//The node isn't toggled just yet. Toggle it immediately
		if(!Toggle[CurRep[u]]){
			Toggle[CurRep[u]]=1;
			Val[CurRep[u]]=w;
		}else{
		//The node is already toggled by an edge with lower edge. Only a fucking dumbass would touch it - already minimized
		//toggle value. Just leave it alone
		}
	}else{
		//u and v doesn't belong to the same component. Merge them
		int CurID=n-1+id;
		int OldComp1=CurRep[u], OldComp2=CurRep[v];
		adj[CurRep[u]].push_back(CurID);
		adj[CurRep[v]].push_back(CurID);
		adj[CurID].push_back(CurRep[u]);
		adj[CurID].push_back(CurRep[v]);
		Par[CurRep[u]]=CurID;
		Par[CurRep[v]]=CurID;
		if(InComponents[u].size()>InComponents[v].size()){
			swap(u,v);
			//Small to large merging
		}
		for(int i=0;i<InComponents[u].size();i++){
			InComponents[CurID].push_back(InComponents[u][i]);
			CurRep[InComponents[u][i]]=CurID;
		}
		for(int i=0;i<InComponents[v].size();i++){
			InComponents[CurID].push_back(InComponents[v][i]);
			CurRep[InComponents[v][i]]=CurID;
		}
		Val[CurID]=w;
		//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;
		}
		KRTID.push_back(CurID);
		RootID=CurID;
	}
}
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++){
		InComponents.push_back(TVector);
		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<n;i++){
		CurRep[i]=i;
		InComponents[i].push_back(i);
		KRTID.push_back(i);
	}
	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;
	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();
	}
	Par[RootID]=500000;
	Par[500000]=500000;
	Toggle[500000]=1;
	for(int i=0;i<=19;i++){
		for(int k=0;k<KRTID.size();k++){
			if(i==0){
				Ancestor[KRTID[k]][i]=Par[KRTID[k]];
			}else{
				Ancestor[KRTID[k]][i]=Ancestor[Ancestor[KRTID[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(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];
		}
	}
	if(Depth[x]!=Depth[y]){
		return -1;
	}
	for(int i=19;i>=0;i--){
		if(Ancestor[x][i]!=Ancestor[y][i]){
			x=Ancestor[x][i];
			y=Ancestor[y][i];
		}
	}
	x=Par[x];
	SkipPoint:;
	if(Toggle[x]){
		return Val[x];
	}
	
	for(int i=19;i>=0;i--){
		if(!Toggle[Ancestor[x][i]]){
			x=Ancestor[x][i];
		}
	}
	x=Par[x];
	if(Toggle[x]&&x!=500000){
		return Val[x];
	}
	return -1;
}

Compilation message

swap.cpp: In function 'void DSU(int, int, int, int)':
swap.cpp:54:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int i=0;i<InComponents[u].size();i++){
      |               ~^~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:58:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   for(int i=0;i<InComponents[v].size();i++){
      |               ~^~~~~~~~~~~~~~~~~~~~~~~
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:100:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |   for(int i=0;i<adj[Cur].size();i++){
      |               ~^~~~~~~~~~~~~~~~
swap.cpp:113:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |   for(int k=0;k<KRTID.size();k++){
      |               ~^~~~~~~~~~~~~
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:148:2: warning: label 'SkipPoint' defined but not used [-Wunused-label]
  148 |  SkipPoint:;
      |  ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12748 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 13012 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 104 ms 50452 KB Output is correct
10 Correct 125 ms 59168 KB Output is correct
11 Correct 120 ms 59416 KB Output is correct
12 Correct 127 ms 60960 KB Output is correct
13 Correct 114 ms 60120 KB Output is correct
14 Correct 96 ms 52004 KB Output is correct
15 Correct 184 ms 61608 KB Output is correct
16 Correct 173 ms 58260 KB Output is correct
17 Correct 199 ms 62084 KB Output is correct
18 Correct 220 ms 60812 KB Output is correct
19 Incorrect 62 ms 25344 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12748 KB Output is correct
3 Correct 194 ms 61116 KB Output is correct
4 Correct 204 ms 64136 KB Output is correct
5 Correct 196 ms 63360 KB Output is correct
6 Correct 197 ms 62076 KB Output is correct
7 Correct 187 ms 62620 KB Output is correct
8 Correct 184 ms 59596 KB Output is correct
9 Correct 210 ms 61596 KB Output is correct
10 Correct 194 ms 59712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12748 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 13012 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Incorrect 2 ms 12788 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 12788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12748 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 13012 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 104 ms 50452 KB Output is correct
10 Correct 125 ms 59168 KB Output is correct
11 Correct 120 ms 59416 KB Output is correct
12 Correct 127 ms 60960 KB Output is correct
13 Correct 114 ms 60120 KB Output is correct
14 Correct 96 ms 52004 KB Output is correct
15 Correct 184 ms 61608 KB Output is correct
16 Correct 173 ms 58260 KB Output is correct
17 Correct 199 ms 62084 KB Output is correct
18 Correct 220 ms 60812 KB Output is correct
19 Correct 194 ms 61116 KB Output is correct
20 Correct 204 ms 64136 KB Output is correct
21 Correct 196 ms 63360 KB Output is correct
22 Correct 197 ms 62076 KB Output is correct
23 Correct 187 ms 62620 KB Output is correct
24 Correct 184 ms 59596 KB Output is correct
25 Correct 210 ms 61596 KB Output is correct
26 Correct 194 ms 59712 KB Output is correct
27 Incorrect 2 ms 12892 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 12788 KB Output isn't correct
2 Halted 0 ms 0 KB -