Submission #1777264
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++) //repeat n times
#define REP2(i,s,n) for(int (i)=(s);(i)<(n);(i)++) //repeat from s to n
#define REPD(i,n) for(int (i)=(n);(i)>=0;(i)--) //repeat from n to 0
#define REPD2(i,s,e) for(int (i)=(s);(i)>=(e);(i)--) //repeat from s to e
#define ASIZE(a) (sizeof(a) / sizeof(a[0])) //array size
#define SORTD(a) sort(a,a+ASIZE(a),greater<int>()) //sort in descending order for array
#define SORTA(a) sort(a,a+ASIZE(a)) //sort in ascending order for array
#define SORTS(a) sort(a.begin(),a.end()) //sort in ascending order for string type
typedef long long LL;
typedef unsigned int UINT;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector< Vi > VVi;
void SCAN(int *a){scanf("%d",a);} //scan for type int
void SCAN(int *a,int n){int i;REP(i,n){scanf("%d",&a[i]);}} //scan for type int array
void SCAN(UINT *a){scanf("%u",a);} //scan for type unsigned int
void SCAN(UINT *a,int n){int i;REP(i,n){scanf("%u",&a[i]);}} //scan for type unsigned int array
void SCAN(LL *a){scanf("%lld",a);} //scan for type long long int
void SCAN(LL *a,int n){int i;REP(i,n){scanf("%lld",&a[i]);}} //scan for type long long int array
void SCAN(char *c){scanf(" %c",c);} //scan for type char
void SCAN(char *c,int n){int i;REP(i,n){scanf(" %c",&c[i]);}} //scan for type char array
const int MOD = 1000000007;
const int INF = 1999999999;
const LL INFLL = 999999999999999;
const int DX4[4]= {-1,0,1,0};
const int DY4[4]= {0,-1,0,1};
//fill an N-dimensional array with val
template<typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val){
fill((T*)array,(T*)(array+N),val);
}
long double pascalTri(int n,int r){
long double tri[n+1][n+1];
int i,j;
REP(i,n+1){REP(j,n+1){tri[i][j]=0;}}
REP(i,n+1){
REP(j,n+1){
if(j>i){break;}
if(j==0||j==i){tri[i][j]=1;}else{tri[i][j]=(tri[i-1][j-1]+tri[i-1][j]);}
}
}
return tri[n][r];
}
Vi graph[100005];
int depth[100005][40][2];
int visit[100005];
void setDepth(int nv,int pa,int d){
visit[nv]=1;
depth[nv][0][0]=pa;
depth[nv][0][1]=d;
int next;
REP(i,graph[nv].size()){
next=graph[nv][i];
if(graph[nv][i]!=pa){
setDepth(next,nv,d+1);
}
}
}
int lca(int a,int b){
int shallow=min(depth[a][0][1],depth[b][0][1]);
int deep=max(depth[a][0][1],depth[b][0][1]);
int s,d;
if(deep==depth[a][0][1]){
d=a;
s=b;
}else{
s=a;
d=b;
}
// cout<<"s "<<depth[s][0][0]<<" "<<depth[s][0][1]<<endl;
// cout<<"d "<<depth[d][0][0]<<" "<<depth[d][0][1]<<endl;
int k=abs(depth[d][0][1]-depth[s][0][1]);
int i=0;
while(k!=0){
if(k%2==1){
if(depth[d][i][0]==INF){
while(depth[s][0][1]!=depth[d][0][1]){
if(depth[d][0][0]==s){
// cout<<"LCA = "<<s<<endl;
return s;
}
d=depth[d][0][0];
}
break;
}
if(depth[d][i][0]==s){
// cout<<"LCA = "<<s<<endl;
return s;
}
d=depth[d][i][0];
}
// cout<<d<<endl;
i++;
k>>=1;
}
// cout<<"s "<<depth[s][0][0]<<" "<<depth[s][0][1]<<endl;
// cout<<"d "<<depth[d][0][0]<<" "<<depth[d][0][1]<<endl;
// k=depth[s][0][1];
// int tmpk=k;
// int tmps=s;
// int tmpd=d;
// i=0;
// while(k!=0){
// if(k%2==1){
// if(depth[d][i][0]==s){
// // cout<<"LCA = "<<s<<endl;
// return s;
// }
// if(depth[d][i][0]==depth[s][i][0]){
// k=--tmpk;
// s=tmps;
// d=tmpd;
// i=-1;
// k<<=1;
// continue;
// }
// s=depth[s][i][0];
// d=depth[d][i][0];
// }
// // cout<<"s "<<s<<endl;
// // cout<<"d "<<d<<endl;
// i++;
// k>>=1;
// }
while(depth[s][0][0]!=depth[d][0][0]){
if(depth[d][0][0]==s){
// cout<<"LCA = "<<s<<endl;
return s;
}
s=depth[s][0][0];
d=depth[d][0][0];
}
// cout<<"LCA = "<<depth[s][0][0]<<endl;
return depth[s][0][0];
}
int main(){
// cin.tie(0);
// ios::sync_with_stdio(false);
int n,q;
int i,j,k;
FILL(visit,0);
FILL(depth,INF);
SCAN(&n);
int x[n],y[n];
REP(i,n-1){
SCAN(&x[i]);SCAN(&y[i]);
x[i]--;y[i]--;
graph[x[i]].push_back(y[i]);
graph[y[i]].push_back(x[i]);
}
SCAN(&q);
int a[q],b[q];
REP(i,q){
SCAN(&a[i]);SCAN(&b[i]);
a[i]--;b[i]--;
}
setDepth(0,-1,0);
int flag=0;
k=1;
while(!flag){
REP2(i,1,n){
flag=1;
if(depth[i][k-1][0]==INF){continue;}
if(depth[i][k-1][0]==-1){continue;}
if(depth[depth[i][k-1][0]][k-1][0]==INF){continue;}
depth[i][k][0]=depth[depth[i][k-1][0]][k-1][0];
depth[i][k][1]=depth[depth[i][k-1][0]][k-1][1];
flag=0;
}
k++;
}
// REP(i,n){
// cout<<i<<" ";
// REP(j,graph[i].size()){
// printf("%d ",graph[i][j]);
// }
// printf("\n");
// }
// REP(i,n){
// cout<<i<<" ";
// REP(k,n){
// if(depth[i][k][0]==INF){break;}
// cout<<depth[i][k][0]<<" "<<depth[i][k][1]<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
int lcaV[q];
REP(i,q){
// cout<<a[i]<<" "<<b[i]<<endl;
lcaV[i]=lca(a[i],b[i]);
// cout<<endl;
}
REP(i,q){
cout<<depth[a[i]][0][1]+depth[b[i]][0][1]-2*depth[lcaV[i]][0][1]+1<<endl;
}
return 0;
}
Submission Info
Submission Time
2017-11-16 17:51:13+0900
Task
D - 閉路
User
nabe12
Language
C++14 (GCC 5.4.1)
Score
100
Code Size
5405 Byte
Status
AC
Exec Time
548 ms
Memory
45824 KB
Compile Error
./Main.cpp: In function ‘void SCAN(int*)’:
./Main.cpp:18:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
void SCAN(int *a){scanf("%d",a);} //scan for type int
^
./Main.cpp: In function ‘void SCAN(int*, int)’:
./Main.cpp:19:57: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
void SCAN(int *a,int n){int i;REP(i,n){scanf("%d",&a[i]);}} //scan for type int array
^
./Main.cpp: In function ‘void SCAN(UINT*)’:
./Main.cpp:20:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
void SCAN(UINT *a){scanf("%u",a);} //scan for type unsigned int
^
./Main.cpp: In function ‘void SCAN(UINT*, int)’:
./Main.cpp:21...
Judge Result
Set Name
Sample
Subtask1
Subtask2
Score / Max Score
0 / 0
30 / 30
70 / 70
Status
Set Name
Test Cases
Sample
subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask1
subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt
Subtask2
subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt
Case Name
Status
Exec Time
Memory
subtask0_sample01.txt
AC
15 ms
34176 KB
subtask0_sample02.txt
AC
15 ms
34176 KB
subtask0_sample03.txt
AC
14 ms
34176 KB
subtask1_01.txt
AC
72 ms
44416 KB
subtask1_02.txt
AC
73 ms
44416 KB
subtask1_03.txt
AC
14 ms
34176 KB
subtask1_04.txt
AC
15 ms
34176 KB
subtask1_05.txt
AC
15 ms
34304 KB
subtask1_06.txt
AC
15 ms
34304 KB
subtask1_07.txt
AC
73 ms
38272 KB
subtask1_08.txt
AC
78 ms
38144 KB
subtask1_09.txt
AC
91 ms
38144 KB
subtask1_10.txt
AC
84 ms
38144 KB
subtask1_11.txt
AC
80 ms
38144 KB
subtask1_12.txt
AC
77 ms
38144 KB
subtask2_01.txt
AC
254 ms
45824 KB
subtask2_02.txt
AC
256 ms
45696 KB
subtask2_03.txt
AC
193 ms
35584 KB
subtask2_04.txt
AC
194 ms
35584 KB
subtask2_05.txt
AC
203 ms
35712 KB
subtask2_06.txt
AC
200 ms
35840 KB
subtask2_07.txt
AC
281 ms
39680 KB
subtask2_08.txt
AC
297 ms
39680 KB
subtask2_09.txt
AC
341 ms
39680 KB
subtask2_10.txt
AC
431 ms
39680 KB
subtask2_11.txt
AC
548 ms
39808 KB
subtask2_12.txt
AC
515 ms
39680 KB