Submission #828101
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pi;
typedef vector<int> vi;
struct UF{
int n;
//正だったらその頂点の親,負だったら根で連結成分の個数
vector<int> d;
UF() {}
UF(int N):n(N), d(N,-1){}
int root(int v){
if(d[v]<0) return v;
return d[v]=root(d[v]);
}
bool unite(int X,int Y){
X=root(X); Y=root(Y);
if(X==Y) return false;
if(size(X) < size(Y)) swap(X,Y);
d[X]+=d[Y];
d[Y]=X;
return true;
}
int size(int v){ return -d[root(v)]; }
};
int main()
{
int n,m;
cin >>n >>m;
vector<int> a(m),b(m);
rep(i,m)
{
scanf(" %d %d", &a[i], &b[i]);
--a[i];
--b[i];
}
int q;
cin >>q;
vector<int> x(q), y(q), z(q);
rep(i,q)
{
scanf(" %d %d %d", &x[i], &y[i], &z[i]);
--x[i];
--y[i];
}
//クエリを並列に二分探索する
vector<int> l(q,0), r(q,m);
rep(times,18)
{
//pivotの位置をクエリごとに分割
vector<vi> s(m+1);
rep(i,q) s[(l[i]+r[i])/2].pb(i);
UF u(n);
//枝を1つずつつないでいく
rep(i,m)
{
u.unite(a[i],b[i]);
for(const auto &j: s[i])
{
int reach=0;
if(u.root(x[j])==u.root(y[j])) reach=u.size(x[j]);
else reach=u.size(x[j])+u.size(y[j]);
if(reach>=z[j]) r[j]=i;
else l[j]=i;
}
}
}
rep(i,q) printf("%d\n", r[i]+1);
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Stamp Rally |
User |
imulan |
Language |
C++14 (GCC 5.4.1) |
Score |
1000 |
Code Size |
1980 Byte |
Status |
AC |
Exec Time |
390 ms |
Memory |
8128 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:45:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf(" %d %d", &a[i], &b[i]);
^
./Main.cpp:55:48: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf(" %d %d %d", &x[i], &y[i], &z[i]);
^
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 |
4 ms |
256 KB |
1_00.txt |
AC |
277 ms |
7744 KB |
1_01.txt |
AC |
317 ms |
7616 KB |
1_02.txt |
AC |
271 ms |
7744 KB |
1_03.txt |
AC |
308 ms |
8000 KB |
1_04.txt |
AC |
274 ms |
7744 KB |
1_05.txt |
AC |
316 ms |
8000 KB |
1_06.txt |
AC |
275 ms |
7744 KB |
1_07.txt |
AC |
324 ms |
8000 KB |
1_08.txt |
AC |
281 ms |
7744 KB |
1_09.txt |
AC |
346 ms |
8128 KB |
1_10.txt |
AC |
294 ms |
7744 KB |
1_11.txt |
AC |
344 ms |
8128 KB |
1_12.txt |
AC |
299 ms |
7616 KB |
1_13.txt |
AC |
343 ms |
8000 KB |
1_14.txt |
AC |
257 ms |
7616 KB |
1_15.txt |
AC |
280 ms |
7616 KB |
1_16.txt |
AC |
322 ms |
7296 KB |
1_17.txt |
AC |
361 ms |
7240 KB |
1_18.txt |
AC |
350 ms |
7012 KB |
1_19.txt |
AC |
328 ms |
6976 KB |
1_20.txt |
AC |
340 ms |
7052 KB |
1_21.txt |
AC |
337 ms |
7124 KB |
1_22.txt |
AC |
363 ms |
7036 KB |
1_23.txt |
AC |
358 ms |
7004 KB |
1_24.txt |
AC |
390 ms |
7292 KB |
1_25.txt |
AC |
355 ms |
7280 KB |
1_26.txt |
AC |
342 ms |
6892 KB |
1_27.txt |
AC |
353 ms |
7272 KB |
1_28.txt |
AC |
342 ms |
6920 KB |
1_29.txt |
AC |
358 ms |
7180 KB |
1_30.txt |
AC |
350 ms |
7152 KB |
1_31.txt |
AC |
341 ms |
7048 KB |