#include <bits/stdc++.h>
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][20];
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);
//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.
//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;
}
}
//cout<<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<=m+n+1;i++){
CurRep[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();
//cout<<Cur<<" ";
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<=19;i++){
for(int k=0;k<n+m;k++){
if(i==0){
Ancestor[k][i]=Par[k];
}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];
}
}
if(x==y){
goto SkipPoint;
}
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];
y=Par[y];
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!=-1){
return Val[x];
}
return -1;
*/
while(Depth[x]>Depth[y]){
x=Par[x];
}
while(x!=y){
x=Par[x];
y=Par[y];
}
while(!Toggle[x]&&x!=-1){
x=Par[x];
}
return Val[x];
}
/*
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;
}
*/
/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
*/
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:97:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=0;i<adj[Cur].size();i++){
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14932 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 |
14968 KB |
Output is correct |
6 |
Correct |
2 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 |
71 ms |
43556 KB |
Output is correct |
10 |
Correct |
114 ms |
50728 KB |
Output is correct |
11 |
Correct |
91 ms |
53680 KB |
Output is correct |
12 |
Correct |
103 ms |
52360 KB |
Output is correct |
13 |
Correct |
90 ms |
53320 KB |
Output is correct |
14 |
Correct |
67 ms |
46288 KB |
Output is correct |
15 |
Correct |
132 ms |
54956 KB |
Output is correct |
16 |
Correct |
119 ms |
54444 KB |
Output is correct |
17 |
Correct |
132 ms |
53668 KB |
Output is correct |
18 |
Correct |
117 ms |
53672 KB |
Output is correct |
19 |
Correct |
64 ms |
23444 KB |
Output is correct |
20 |
Correct |
154 ms |
54052 KB |
Output is correct |
21 |
Correct |
154 ms |
51440 KB |
Output is correct |
22 |
Correct |
160 ms |
55732 KB |
Output is correct |
23 |
Execution timed out |
2037 ms |
53652 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14932 KB |
Output is correct |
3 |
Execution timed out |
2057 ms |
54932 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14932 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 |
14968 KB |
Output is correct |
6 |
Correct |
2 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 |
Incorrect |
2 ms |
14696 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
14696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14932 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 |
14968 KB |
Output is correct |
6 |
Correct |
2 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 |
71 ms |
43556 KB |
Output is correct |
10 |
Correct |
114 ms |
50728 KB |
Output is correct |
11 |
Correct |
91 ms |
53680 KB |
Output is correct |
12 |
Correct |
103 ms |
52360 KB |
Output is correct |
13 |
Correct |
90 ms |
53320 KB |
Output is correct |
14 |
Correct |
67 ms |
46288 KB |
Output is correct |
15 |
Correct |
132 ms |
54956 KB |
Output is correct |
16 |
Correct |
119 ms |
54444 KB |
Output is correct |
17 |
Correct |
132 ms |
53668 KB |
Output is correct |
18 |
Correct |
117 ms |
53672 KB |
Output is correct |
19 |
Execution timed out |
2057 ms |
54932 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
14696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |