#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int BUC = 700;
const int CNTB = 290;
const int MAXN = 100000;
struct str_DSU
{
int father[MAXN+5],siz[MAXN+5],cap1[MAXN+5],cap2[MAXN+5],islant[MAXN+5];
vector<pair<int*,int>> history;
void init(int N)
{
for(int i=0;i<N;i++)
{
siz[i]=1;
father[i]=cap1[i]=cap2[i]=i;
islant[i]=1;
}
}
int gas(int x)
{
if(father[x]==x)
return x;
return gas(father[x]);
}
void unite(int x, int y)
{
int rootx = gas(x);
int rooty = gas(y);
if(rootx==rooty)
{
if(islant[rootx])
{
history.push_back({&islant[rootx],islant[rootx]});
islant[rootx]=0;
}
return;
}
if(siz[rootx] < siz[rooty])
{
swap(rootx,rooty);
swap(x,y);
}
history.push_back({&father[y],father[y]});
history.push_back({&siz[x],siz[x]});
father[y]=x;
siz[x]+=siz[y];
if(!islant[rootx] || !islant[rooty])
{
if(islant[rootx])
{
history.push_back({&islant[rootx],islant[rootx]});
islant[rootx]=0;
}
}
else if(x==cap1[rootx] && y==cap1[rooty])
{
history.push_back({&cap1[rootx],cap1[rootx]});
cap1[rootx] = cap2[rooty];
}
else if(x==cap1[rootx] && y==cap2[rooty])
{
history.push_back({&cap1[rootx],cap1[rootx]});
cap1[rootx] = cap1[rooty];
}
else if(x==cap2[rootx] && y==cap1[rooty])
{
history.push_back({&cap2[rootx],cap2[rootx]});
cap2[rootx] = cap2[rooty];
}
else if(x==cap2[rootx] && y==cap2[rooty])
{
history.push_back({&cap2[rootx],cap2[rootx]});
cap2[rootx] = cap1[rooty];
}
else
{
history.push_back({&islant[rootx],islant[rootx]});
islant[rootx]=0;
}
}
void rollback(int t)
{
while((int)history.size() > t)
{
*history.back().first = history.back().second;
history.pop_back();
}
}
};
str_DSU saves[CNTB+5];
vector<int> pozs;
str_DSU dsu;
void do_save(int i, int N)
{
//cout<<i<<" save\n";
saves[(int)pozs.size()] = dsu;
pozs.push_back(i);
}
vector<pair<int,pair<int,int>>> edges;
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
for(int i=0;i<M;i++) edges.push_back({W[i],{U[i],V[i]}});
sort(edges.begin(),edges.end());
dsu.init(N);
do_save(-1,N);
for(int i=0;i<M;i++)
{
dsu.unite(edges[i].second.first, edges[i].second.second);
if((i+1)%BUC==0 || i==M-1)
{
do_save(i,N);
}
}
}
int getMinimumFuelCapacity(int X, int Y)
{
int ults=0;
for(int i=1;i<pozs.size();i++)
{
if(saves[i].gas(X)==saves[i].gas(Y) && saves[i].islant[saves[i].gas(X)]==0)
break;
ults=i;
}
if(ults==(int)pozs.size()-1)
return -1;
//str_DSU cop = saves[ults];///-------------------------
int t = saves[ults].history.size();
for(int i=pozs[ults]+1;i<edges.size();i++)
{
int a = edges[i].second.first, b = edges[i].second.second;
//cout<<a<<" "<<b<<" baga\n";
saves[ults].unite(a, b);
if(saves[ults].gas(X)==saves[ults].gas(Y) && saves[ults].islant[saves[ults].gas(X)]==0)
{
saves[ults].rollback(t);
//saves[ults] = cop;///--------------------------
return edges[i].first;
break;
}
}
assert(1==0);
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:125:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(int i=1;i<pozs.size();i++)
| ~^~~~~~~~~~~~
swap.cpp:135:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int i=pozs[ults]+1;i<edges.size();i++)
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6524 KB |
Output is correct |
4 |
Correct |
3 ms |
6748 KB |
Output is correct |
5 |
Correct |
5 ms |
8664 KB |
Output is correct |
6 |
Correct |
4 ms |
8796 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8796 KB |
Output is correct |
9 |
Correct |
200 ms |
445420 KB |
Output is correct |
10 |
Runtime error |
250 ms |
524288 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Execution timed out |
2101 ms |
494808 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6524 KB |
Output is correct |
4 |
Correct |
3 ms |
6748 KB |
Output is correct |
5 |
Correct |
5 ms |
8664 KB |
Output is correct |
6 |
Correct |
4 ms |
8796 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
6492 KB |
Output is correct |
10 |
Incorrect |
4 ms |
8636 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6524 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
5 ms |
8664 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
6 ms |
8796 KB |
Output is correct |
10 |
Correct |
200 ms |
445420 KB |
Output is correct |
11 |
Runtime error |
250 ms |
524288 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6524 KB |
Output is correct |
4 |
Correct |
3 ms |
6748 KB |
Output is correct |
5 |
Correct |
5 ms |
8664 KB |
Output is correct |
6 |
Correct |
4 ms |
8796 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
6 ms |
8796 KB |
Output is correct |
9 |
Correct |
200 ms |
445420 KB |
Output is correct |
10 |
Runtime error |
250 ms |
524288 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6524 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
5 ms |
8664 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
6 ms |
8796 KB |
Output is correct |
10 |
Correct |
200 ms |
445420 KB |
Output is correct |
11 |
Runtime error |
250 ms |
524288 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |