結果
| 問題 |
No.363 門松サイクル
|
| コンテスト | |
| ユーザー |
yaoshimax
|
| 提出日時 | 2016-04-18 18:39:23 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,085 bytes |
| コンパイル時間 | 607 ms |
| コンパイル使用メモリ | 70,892 KB |
| 実行使用メモリ | 18,468 KB |
| 最終ジャッジ日時 | 2024-10-04 13:47:30 |
| 合計ジャッジ時間 | 4,821 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 4 WA * 23 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:60:9: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
60 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
main.cpp:67:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
67 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:116:9: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
116 | scanf("%d",&Q);
| ~~~~~^~~~~~~~~
main.cpp:119:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
119 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <climits>
#include <vector>
#include <cstdio>
#include <cstring>
#include <set>
#include <stack>
using namespace std;
bool kadomatsu(int x,int y, int z){
if( x==y || y==z || z==x ) return false;
if( x<y && y>z ) return true;
if( x>y && y<z) return true;
return false;
}
int depth[100000];
int parent[17][100000];
int val[100000];
bool iskadsequence[17][100000];
vector<int> edge[100000];
int up(int cur, int d){
if( d==0 ) return cur;
int ex=0;
int ret = cur;
while( (1<<ex)*2 <= d ) ex++;
while( d!= 0&&ex>=0 ){
if( (d&(1<<ex))!=0){
ret = parent[ex][ret];
d-=(1<<ex);
}
ex--;
}
return ret;
}
int lca(int u, int v){
if( u==v ) return u;
int left=0;
int right=depth[u];
while( left+1 < right ){
int mid=(left+right)/2;
if( up(u,mid)==up(v,mid) ){
right = mid;
}
else{
left=mid;
}
}
return up(u,right);
}
bool checkKadSeq(int u, int d){
if( d<= 1 ) return true;
int ex=0;
while( (1<<ex)*2 <= d ) ex++;
if( iskadsequence[ex][u]== false ) return false;
else return checkKadSeq(parent[ex][u], d-(1<<ex));
}
int main(){
int N;
scanf("%d",&N);
memset(depth,-1,sizeof(depth));
memset(parent,-1,sizeof(parent));
memset( iskadsequence,false,sizeof(iskadsequence));
for( int i = 0 ; i < N; i++ ) cin >> val[i];
for( int i = 0 ; i <N-1; i++ ){
int x,y;
scanf("%d%d",&x,&y);
x--;y--;
edge[x].push_back(y);
edge[y].push_back(x);
}
stack<int> st;
st.push(0);
depth[0]=0;
parent[0][0]=-1;
while( !st.empty() ){
int cur = st.top();
st.pop();
for( int ei=0; ei < (int)edge[cur].size(); ei++){
int nxt = edge[cur][ei];
if( depth[nxt]!=-1 ) continue;
depth[nxt]=depth[cur]+1;
parent[0][nxt]=cur;
iskadsequence[0][nxt]=true;
int tmpCur=cur;
int tmpNxt=nxt;
for( int i = 0;parent[i][tmpCur]!=-1;i++){
parent[i+1][nxt]=parent[i][tmpCur];
iskadsequence[i+1][nxt]=(iskadsequence[i][nxt]&iskadsequence[i][tmpCur]&kadomatsu(val[parent[0][tmpCur]],val[tmpCur],val[tmpNxt]));
tmpCur=parent[i][tmpCur];
tmpNxt=parent[i][tmpNxt];
}
st.push(nxt);
}
}
/*
for( int i = 0 ; i < N; i++ ){
cout << depth[i]<<", ";
}
cout << endl;
for( int i = 0 ; i <N; i++ ){
cout <<i<<": ";
for( int j = 0 ; j < 17; j++ ){
if( parent[j][i]!=-1 ) cout << (1<<j) <<"="<<parent[j][i];
}
cout << endl;
}
for( int i = 0 ; i <N; i++ ){
cout <<i<<": ";
for( int j = 0 ; j < 17; j++ ){
if( parent[j][i]!=-1 ) cout << (1<<j) <<"="<<iskadsequence[j][i];
}
cout << endl;
}*/
int Q;
scanf("%d",&Q);
for( int i = 0 ; i < Q; i++ ){
int u,v;
scanf("%d%d",&u,&v);
u--;
v--;
if( depth[u] > depth[v] ){
swap(u,v);
}
if( parent[0][u]==v || parent[0][v]== u){
printf("NO\n");
continue;
}
int v2=up(v,depth[v]-depth[u]);
if( v2 == u ){
int child_v=up(v,depth[v]-depth[u]-1);
if( checkKadSeq(v,depth[v]-depth[u])
&& kadomatsu(val[parent[0][v]],val[v],val[u])
&& kadomatsu(val[child_v],val[u],val[v])){
printf("YES\n");
}
else{
printf("NO\n");
}
continue;
}
int p=lca(u,v2);
int child_u=up(u,depth[u]-depth[p]-1);
int child_v=up(v,depth[v]-depth[p]-1);
//cout << v2<<", "<<p <<", "<< child_u << ", "<< child_v<<endl;
if( kadomatsu(val[child_u],val[p],val[child_v])
&& checkKadSeq(u,depth[u]-depth[p])
&& checkKadSeq(v,depth[u]-depth[p])
&& kadomatsu(val[u],val[v],val[parent[0][v]])
&& kadomatsu(val[v],val[u],val[parent[0][u]])
){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}
yaoshimax