Submission #8520861
Source Code Expand
#include<bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,m,q,a[MAXN],b[MAXN];
int p[MAXN],r[MAXN],sz[MAXN];
int ans[MAXN];
struct update
{
int x,y;
bool addrk;
};
struct query
{
int x,y,z,id;
};
update st[MAXN];
int t;
void init(int n)
{
for(int i=1;i<=n;i++)
{
p[i]=i;
r[i]=0;
sz[i]=1;
}
}
int find(int x)
{
while(p[x]!=x) x=p[x];
return x;
}
bool unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y) return false;
if(r[x]<r[y])
{
p[x]=y;
sz[y]+=sz[x];
st[t++]=(update){x,y,false};
}
else
{
p[y]=x; sz[x]+=sz[y];
st[t++]=(update){y,x,r[x]==r[y]};
if(r[x]==r[y]) r[x]++;
}
return true;
}
void undo()
{
assert(t);
int x=st[t-1].x,y=st[t-1].y;
p[x]=x;p[y]=y;
sz[y]-=sz[x];
if(st[t-1].addrk) r[y]--;
t--;
}
bool same(int x,int y)
{
return find(x)==find(y);
}
void solve(int l,int r,vector<query> &v)
{
if(l==r)
{
for(auto p:v) ans[p.id]=l;
return;
}
vector<query> lhs,rhs;
int mid=(l+r)/2,cnt=0;
for(int i=l;i<=mid;i++) if(unite(a[i],b[i])) cnt++;
for(auto p:v)
{
int x=find(p.x),y=find(p.y);
int res=(same(x,y)?sz[x]:sz[x]+sz[y]);
if(p.z<=res) lhs.push_back(p); else rhs.push_back(p);
}
for(int i=0;i<cnt;i++) undo();
solve(l,mid,lhs);
for(int i=l;i<=mid;i++) unite(a[i],b[i]);
solve(mid+1,r,rhs);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d%d",&a[i],&b[i]);
scanf("%d",&q);
vector<query> v;
for(int i=1;i<=q;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
v.push_back((query){x,y,z,i});
}
init(n);
solve(1,m,v);
for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Stamp Rally |
User |
Roundgod |
Language |
C++14 (GCC 5.4.1) |
Score |
1000 |
Code Size |
2064 Byte |
Status |
AC |
Exec Time |
182 ms |
Memory |
19644 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:93:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
./Main.cpp:94:52: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=m;i++) scanf("%d%d",&a[i],&b[i]);
^
./Main.cpp:95:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
^
./Main.cpp:100:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&x,&y,&z);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1000 / 1000 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_00.txt |
All |
0_00.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt |
Case Name |
Status |
Exec Time |
Memory |
0_00.txt |
AC |
1 ms |
256 KB |
1_00.txt |
AC |
108 ms |
8816 KB |
1_01.txt |
AC |
119 ms |
9968 KB |
1_02.txt |
AC |
107 ms |
8944 KB |
1_03.txt |
AC |
118 ms |
15728 KB |
1_04.txt |
AC |
107 ms |
9072 KB |
1_05.txt |
AC |
121 ms |
14064 KB |
1_06.txt |
AC |
108 ms |
8944 KB |
1_07.txt |
AC |
122 ms |
12016 KB |
1_08.txt |
AC |
110 ms |
9072 KB |
1_09.txt |
AC |
124 ms |
10224 KB |
1_10.txt |
AC |
115 ms |
8816 KB |
1_11.txt |
AC |
128 ms |
9968 KB |
1_12.txt |
AC |
121 ms |
8944 KB |
1_13.txt |
AC |
133 ms |
9968 KB |
1_14.txt |
AC |
112 ms |
9968 KB |
1_15.txt |
AC |
119 ms |
10096 KB |
1_16.txt |
AC |
175 ms |
19644 KB |
1_17.txt |
AC |
179 ms |
12660 KB |
1_18.txt |
AC |
179 ms |
12788 KB |
1_19.txt |
AC |
175 ms |
18072 KB |
1_20.txt |
AC |
171 ms |
12788 KB |
1_21.txt |
AC |
175 ms |
15080 KB |
1_22.txt |
AC |
177 ms |
12532 KB |
1_23.txt |
AC |
181 ms |
12848 KB |
1_24.txt |
AC |
182 ms |
11584 KB |
1_25.txt |
AC |
180 ms |
13520 KB |
1_26.txt |
AC |
174 ms |
14324 KB |
1_27.txt |
AC |
179 ms |
13028 KB |
1_28.txt |
AC |
175 ms |
14068 KB |
1_29.txt |
AC |
180 ms |
10992 KB |
1_30.txt |
AC |
178 ms |
12280 KB |
1_31.txt |
AC |
175 ms |
12788 KB |