結果
| 問題 |
No.1295 木と駒
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2020-05-26 15:36:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 329 ms / 2,000 ms |
| コード長 | 2,373 bytes |
| コンパイル時間 | 3,059 ms |
| コンパイル使用メモリ | 207,008 KB |
| 最終ジャッジ日時 | 2025-01-10 15:43:11 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 48 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:77:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
77 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
main.cpp:83:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
83 | scanf("%d %d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000001
struct subtree{
int mini=Inf,maxi=-Inf;
int NGid=-1;
bool ok=true;
};
template <typename T,typename F0,typename F1>
struct rerooting{
F0 func0;
F1 func1;
T init_value;
vector<T> v;
vector<T> ans;
vector<bool> visited;
rerooting(vector<vector<int>> &E,F0 f0,F1 f1,T iv):func0(f0),func1(f1){
init_value = iv;
v.resize(E.size());
ans.resize(E.size());
visited.resize(E.size(),false);
for(int i=0;i<E.size();i++){
if(visited[i])continue;
dfs(E,i,-1);
redfs(E,i,-1,init_value);
}
}
void dfs(vector<vector<int>> &E,int now,int p){
v[now] = init_value;
for(int i=0;i<E[now].size();i++){
int to = E[now][i];
if(to==p)continue;
dfs(E,to,now);
v[now] = func0(v[now],v[to]);
}
v[now] = func1(v[now],now,p);
visited[now]=true;
}
void redfs(vector<vector<int>> &E,int now,int p,T pv){
vector<T> S(E[now].size(),init_value);
if(S.size()>1){
for(int i=S.size()-2;i>=0;i--){
int to = E[now][i+1];
T x = v[to];
if(to==p)x = pv;
S[i] = func0(x,S[i+1]);
}
}
T temp = init_value;
for(int i=0;i<E[now].size();i++){
int to = E[now][i];
if(to!=p){
redfs(E,to,now,func1(func0(temp,S[i]),now,to));
temp = func0(temp,v[to]);
}
else{
temp = func0(temp,pv);
}
}
ans[now] = func1(temp,now,p);
}
};
int main(){
int N;
scanf("%d",&N);
vector<vector<int>> E(N,vector<int>());
for(int i=0;i<N-1;i++){
int u,v;
scanf("%d %d",&u,&v);
u--;v--;
E[u].push_back(v);
E[v].push_back(u);
}
auto f0 = [](subtree a,subtree b){
subtree ret = a;
ret.mini = min(ret.mini,b.mini);
ret.maxi = max(ret.maxi,b.maxi);
if(!b.ok)ret.ok = false;
if(b.NGid!=-1){
if(ret.NGid==-1)ret.NGid=b.NGid;
else ret.ok=false;
}
return ret;
};
auto f1 = [](subtree a,int ind,int p){
subtree ret = a;
if(ret.NGid!=-1){
if(ret.NGid!=ret.maxi && (ret.NGid!=ret.mini || p<ret.mini))ret.ok = false;
ret.NGid=ind;
}
else{
if(ret.mini<p)ret.NGid = ind;
}
ret.mini = ind;
ret.maxi = ind;
return ret;
};
rerooting<subtree,decltype(f0),decltype(f1)> rr(E,f0,f1,subtree());
for(int i=0;i<N;i++){
if(rr.ans[i].ok)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
沙耶花