제출 #421388

#제출 시각아이디문제언어결과실행 시간메모리
421388codebuster_10Railway (BOI17_railway)C++17
100 / 100
469 ms111648 KiB
#include <bits/stdc++.h> using namespace std ; #define int int64_t //be careful about this #define endl "\n" #define f(i,a,b) for(int i=int(a);i<int(b);++i) #define pr pair #define ar array #define fr first #define sc second #define vt vector #define pb push_back #define eb emplace_back #define LB lower_bound #define UB upper_bound #define PQ priority_queue #define sz(x) ((int)(x).size()) #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define mem(a,b) memset(a, b, sizeof(a)) template<class A> void rd(vt<A>& v); template<class T> void rd(T& x){ cin >> x; } template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;} template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;} template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<typename T> void __p(T a) { cout<<a; } template<typename T, typename F> void __p(pair<T, F> a) { cout<<"{"; __p(a.first); cout<<","; __p(a.second); cout<<"}\n"; } template<typename T> void __p(std::vector<T> a) { cout<<"{"; for(auto it=a.begin(); it<a.end(); it++) __p(*it),cout<<",}\n"[it+1==a.end()]; } template<typename T, typename ...Arg> void __p(T a1, Arg ...a) { __p(a1); __p(a...); } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "; __p(arg1); cout<<endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(;; i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; const char *comma=names+i; cout.write(names,comma-names)<<" : "; __p(arg1); cout<<" | "; __f(comma+1,args...); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); cout.precision(15); cout << fixed; if(sz(s)){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } } struct LCA { int N, timer = 0, LOG ; vector< vector<int> > up, dis, max_edge ; vector< vector<array<int,2> > > adj ; vector<int> tin, tout, depth ; void init(int _N) { N = _N ; LOG = log2(N) + 5 ; adj.resize(N) ; up.resize(N, vector<int> (LOG)) ; dis.resize(N, vector<int> (LOG)) ; max_edge.resize(N, vector<int> (LOG)) ; tin = tout = depth = vector<int> (N) ; } void ae(int x, int y,int w = 1) { // add edge adj[x].push_back({y,w}), adj[y].push_back({x,w}); } void dfs(int v, int p, int w){ tin[v] = ++timer; up[v][0] = p; dis[v][0] = w ; max_edge[v][0] = w ; for(int i = 1; i < LOG; i++){ up[v][i] = up[up[v][i-1]][i-1] ; dis[v][i] = dis[v][i-1] + dis[up[v][i-1]][i-1] ; max_edge[v][i] = max(max_edge[v][i-1],max_edge[up[v][i-1]][i-1]) ; } for(auto arr:adj[v]){ int u = arr[0], w = arr[1]; if(u != p){ depth[u] = depth[v] + 1 ; dfs(u,v,w) ; } } tout[v] = ++timer; } void gen(int R = 0) { depth[R] = 0 ; dfs(R,R,0); } bool is_ancestor(int u, int v){ return tin[u] <= tin[v] && tout[u] >= tout[v]; } int lca(int u, int v){ if( is_ancestor(u,v) ) return u; if( is_ancestor(v,u) ) return v; for(int i=LOG-1;i>=0;--i) { if( !is_ancestor(up[u][i],v) ){ u = up[u][i] ; } } return up[u][0]; } int dist(int u, int v) { return depth[u] + depth[v] - 2*depth[lca(u,v)]; } int weighted_dist(int u,int v){ int temp = lca(u,v); int x1 = kth_ancestor_weighted_dist(u,depth[u] - depth[temp]); int x2 = kth_ancestor_weighted_dist(v,depth[v] - depth[temp]); return x1 + x2 ; } int weighted_max_edge(int u,int v){ int temp = lca(u,v); int x1 = kth_ancestor_weighted_max_edge(u,depth[u] - depth[temp]); int x2 = kth_ancestor_weighted_max_edge(v,depth[v] - depth[temp]); return max(x1, x2) ; } int kth_ancestor(int i,int k){ if(depth[i] < k) return int(-1) ; for(int j = LOG - 1; j >= 0; --j){ if( (int(1) << j) <= k ){ k -= (int(1) << j) ; i = up[i][j] ; } } return i ; } int kth_ancestor_weighted_dist(int i,int k){ if(depth[i] < k) return int(-1) ; int ans = 0 ; for(int j = LOG - 1; j >= 0; --j){ if( (int(1) << j) <= k ){ k -= (int(1) << j) ; ans += dis[i][j] ; i = up[i][j] ; } } return ans ; } int kth_ancestor_weighted_max_edge(int i,int k){ if(depth[i] < k) return int(-1) ; int ans = 0 ; for(int j = LOG - 1; j >= 0; --j){ if( (int(1) << j) <= k ){ k -= (int(1) << j) ; ckmax(ans,max_edge[i][j]) ; i = up[i][j] ; } } return ans ; } int d(int i){ return depth[i]; } }; LCA tree; const int MAX_N = 1e5 + 7; vector<int> g[MAX_N], st(MAX_N), en(MAX_N), par(MAX_N); int timer = 1; void dfs(int i, int p){ st[i] = timer++ ; par[i] = p; for(auto j:g[i]) if(j!=p){ dfs(j, i); } en[i] = timer - 1 ; } template <class T, int ...Ns> struct BIT { T val = 0; void upd(T v) { val += v; } T query() { return val; } }; template <class T, int N, int... Ns> struct BIT<T, N, Ns...> { BIT<T,Ns...> bit[N+1]; template<typename... Args> void upd(int pos, Args... args) { assert(pos > 0); for (; pos<=N; pos+=pos&-pos) bit[pos].upd(args...); } template<typename... Args> T sum(int r, Args... args) { T res=0; for (;r;r-=r&-r) res += bit[r].query(args...); return res; } template<typename... Args> T query(int l, int r, Args... args) { return sum(r,args...)-sum(l-1,args...); } }; BIT<int,MAX_N> bit; signed main(){ setIO(); int n,m,k; rd(n,m,k); tree.init(n); vt<ar<int,2>> edges; f(_,0,n-1){ int u,v; rd(u,v); edges.pb({--u,--v}); tree.ae(u,v); g[u].pb(v); g[v].pb(u); } tree.gen(0); dfs(0,0); set<pr<int,int>> node[n]; f(j,0,m){ int SZ; rd(SZ); vt<int> a(SZ); for(auto& i : a) rd(i),--i; int x = a[0]; for(auto i : a){ x = tree.lca(i,x); bit.upd(st[i],1);// ude bit to get end points. } int d = tree.d(x); for(auto i : a){ if(bit.query(st[i],en[i]) == 1){ node[i].insert({-d,j}); } } for(auto i : a) bit.upd(st[i],-1); } vt<int> value(n); function<void(int)> small_to_large = [&](int i){ for(auto j : g[i]) if(j != par[i]){ small_to_large(j); // merge j and i; if( sz(node[j]) > sz(node[i]) ) node[i].swap(node[j]); for(auto p : node[j]) node[i].insert(p); node[j].clear(); } int d = -1 * tree.d(i); while(sz(node[i]) && node[i].begin()->first <= d){ node[i].erase(node[i].begin()); } value[i] = sz(node[i]); }; small_to_large(0); set<pr<int,int>> good; f(i,0,n) if(value[i] >= k) good.insert({i,par[i]}); vt<int> ans; f(i,0,n-1){ auto [u,v] = edges[i]; if(good.count({u,v}) || good.count({v,u})) ans.pb(i); } cout << sz(ans) << endl; for(auto i : ans) cout << i + 1 << " " ; }

컴파일 시 표준 에러 (stderr) 메시지

railway.cpp: In function 'void setIO(std::string)':
railway.cpp:84:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |    freopen((s+".in").c_str(),"r",stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:85:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |    freopen((s+".out").c_str(),"w",stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...