結果
問題 | No.363 門松サイクル |
ユーザー | koyumeishi |
提出日時 | 2016-03-25 11:57:10 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 436 ms / 4,000 ms |
コード長 | 4,353 bytes |
コンパイル時間 | 1,260 ms |
コンパイル使用メモリ | 107,088 KB |
実行使用メモリ | 41,856 KB |
最終ジャッジ日時 | 2024-10-02 00:46:34 |
合計ジャッジ時間 | 7,229 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 3 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 3 ms
5,248 KB |
testcase_08 | AC | 3 ms
5,248 KB |
testcase_09 | AC | 91 ms
18,176 KB |
testcase_10 | AC | 85 ms
19,072 KB |
testcase_11 | AC | 224 ms
36,864 KB |
testcase_12 | AC | 250 ms
33,920 KB |
testcase_13 | AC | 195 ms
31,488 KB |
testcase_14 | AC | 144 ms
28,416 KB |
testcase_15 | AC | 210 ms
34,816 KB |
testcase_16 | AC | 201 ms
25,216 KB |
testcase_17 | AC | 436 ms
41,856 KB |
testcase_18 | AC | 235 ms
34,176 KB |
testcase_19 | AC | 169 ms
34,816 KB |
testcase_20 | AC | 220 ms
34,176 KB |
testcase_21 | AC | 386 ms
39,936 KB |
testcase_22 | AC | 193 ms
33,408 KB |
testcase_23 | AC | 332 ms
33,920 KB |
testcase_24 | AC | 1 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 310 ms
41,856 KB |
testcase_27 | AC | 324 ms
41,856 KB |
testcase_28 | AC | 205 ms
38,016 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:87:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 87 | scanf("%d", &a[i]); | ~~~~~^~~~~~~~~~~~~ main.cpp:97:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 97 | scanf("%d%d", &u,&v); | ~~~~~^~~~~~~~~~~~~~~ main.cpp:188:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 188 | scanf("%d%d", &u,&v); | ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <functional> #include <set> #include <ctime> #include <random> #include <chrono> using namespace std; template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;} template<class T> istream& operator , (istream& is, T& val){ return is >> val;} template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;} template<class T> ostream& operator , (ostream& os, const T& val){ return os << " " << val;} template<class T> ostream& operator >> (ostream& os, const T& val){ return os << " " << val;} bool is_kadomatsu(int a, int b, int c){ return a!=c && ((a<b&&b>c) || (a>b&&b<c)); } class UnionFindTree{ struct base_node{ int parent; int rank; int size; }; vector<base_node> node; public: UnionFindTree(int n){ node.resize(n); for(int i=0; i<n; i++){ node[i].parent=i; node[i].rank=0; node[i].size=1; } } int find(int x){ //return root node of x if(node[x].parent == x) return x; else{ return node[x].parent = find(node[x].parent); } } bool same(int x, int y){ return find(x) == find(y); } int size(int at){ return node[find(at)].size; } void unite(int x, int y){ x = find(node[x].parent); y = find(node[y].parent); if(x==y) return; if(node[x].rank < node[y].rank){ node[x].parent = y; node[y].size += node[x].size; }else if(node[x].rank > node[y].rank){ node[y].parent = x; node[x].size += node[y].size; }else{ node[x].rank++; unite(x,y); } } }; #include <cassert> int main(){ int n; cin >> n; assert(2<=n && n<=100000); vector<int> a(n); //cin >> a; for(int i=0; i<n; i++){ scanf("%d", &a[i]); assert(1<=a[i] && a[i]<=100000); } UnionFindTree uft(n); vector<vector<int>> G(n); for(int i=0; i<n-1; i++){ int u,v; //cin >> u,v; scanf("%d%d", &u,&v); assert(1<=u && u<=n); assert(1<=v && v<=n); assert(u!=v); u--; v--; uft.unite(u,v); G[u].push_back(v); G[v].push_back(u); } assert(uft.size(0) == n); vector<vector<int>> par(n, vector<int>(20)); vector<vector<int>> kad(n, vector<int>(20)); vector<int> dep(n, -1); function<void(int,int)> dfs = [&](int pos, int last){ dep[pos] = dep[last]+1; par[pos][0] = last; if(is_kadomatsu(a[pos], a[last], a[par[last][0]])){ kad[pos][0] = 1; }else{ kad[pos][0] = 0; } for(int next : G[pos]){ if(next == last) continue; dfs(next, pos); } }; dfs(0,0); for(int k=1; k<20; k++){ for(int i=0; i<n; i++){ kad[i][k] = kad[i][k-1] & kad[par[i][k-1]][k-1]; par[i][k] = par[par[i][k-1]][k-1]; } } auto k_anc = [&](int pos, int k){ int b=0; while(k){ if(k&1) pos = par[pos][b]; b++; k >>= 1; } return pos; }; auto lca = [&](int u, int v){ if(dep[u] < dep[v]) swap(u,v); u = k_anc(u, dep[u]-dep[v]); if(u==v) return u; for(int k=19; k>=0; k--){ if(par[u][k] != par[v][k]){ u = par[u][k]; v = par[v][k]; } } return par[u][0]; }; auto kadomatsu_line = [&](int u, int p) -> bool{ int d = dep[u] - dep[p] - 2; if(d<0) return true; d++; int ret = kad[u][0]; int b = 0; while(d>0){ if(d&1){ ret &= kad[u][b]; u = par[u][b]; } b++; d>>=1; } return ret; }; int q; cin >> q; assert(1<=q && q<=100000); while(q--){ int u,v; //cin >> u,v; scanf("%d%d", &u,&v); assert(1<=u && u<=n); assert(1<=v && v<=n); u--; v--; assert(u!=v); if(dep[u] < dep[v]) swap(u,v); int p = lca(u,v); if(p==v){ int pu = k_anc(u, dep[u]-dep[p]-1); int qu = par[u][0]; bool ok = ( is_kadomatsu(a[pu],a[p],a[u]) && is_kadomatsu(a[p],a[u],a[qu]) && kadomatsu_line(u, p) ); cout << (ok?"YES":"NO") << endl; }else{ int pu = k_anc(u, dep[u]-dep[p]-1); int pv = k_anc(v, dep[v]-dep[p]-1); int qu = par[u][0]; int qv = par[v][0]; bool ok = ( is_kadomatsu(a[pu], a[p], a[pv]) && is_kadomatsu(a[qu], a[u], a[v]) && is_kadomatsu(a[u], a[v], a[qv]) && kadomatsu_line(u,p) && kadomatsu_line(v,p) ); //cout << (ok?"YES":"NO") << endl; puts(ok?"YES":"NO"); } } return 0; }