Submission #1141888

#TimeUsernameProblemLanguageResultExecution timeMemory
1141888KasymKBalanced Tree (info1cup18_balancedtree)C++20
0 / 100
4091 ms4188 KiB
#include "bits/stdc++.h" using namespace std; #define ff first #define ss second #define all(v) v.begin(), v.end() #define ll long long #define pb push_back #define pii pair<int, int> #define pli pair<ll, int> #define pll pair<ll, ll> #define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i) #define wr puts("----------------") template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} const int N = 1e5+5; const int INF = 1e9; vector<int> adj[N]; int val[N], d[N], d2[N], n, same[N]; bool vis[N]; int fnd(){ if(count(val+1, val+n+1, 0)==0 or count(val+1, val+n+1, 1)==0) return 1; if(count(val+1, val+n+1, 0)==1 or count(val+1, val+n+1, 1)==1) return INF; for(int i = 1; i <= n; ++i) d[i]=INF, d2[i]=INF, vis[i]=0; queue<int> q; for(int i = 1; i <= n; ++i) if(val[i]==0) q.push(i); while(!q.empty()){ int x=q.front(); q.pop(); tr(it, adj[x]) if(!vis[*it]) d[*it]=(d[x]!=INF?d[x]+1:1), vis[*it]=1, q.push(*it); } for(int i = 1; i <= n; ++i){ vis[i]=0; if(val[i]==1) q.push(i); } while(!q.empty()){ int x=q.front(); q.pop(); tr(it, adj[x]) if(!vis[*it]) d2[*it]=(d2[x]!=INF?d2[x]+1:1), vis[*it]=1, q.push(*it); } int D=-INF; for(int i = 1; i <= n; ++i) if(val[i]==0) umax(D, d[i]); else umax(D, d2[i]); return D; } void solve(){ scanf("%d", &n); for(int i = 0; i <= n; ++i) adj[i].clear(); for(int i = 1; i < n; ++i){ int a, b; scanf("%d%d", &a, &b); adj[a].pb(b); adj[b].pb(a); } for(int i = 1; i <= n; ++i) scanf("%d", val+i), same[i]=val[i]; int answer=INF; vector<int> ans; for(int mk = 0; mk < (1<<n); ++mk){ for(int i = 0; i < n; ++i){ if(val[i+1]!=-1) continue; val[i+1]=(mk>>i&1); } if(umin(answer, fnd())) for(int i = 1; i <= n; ++i) ans.pb(val[i]); for(int i = 1; i <= n; ++i) val[i]=same[i]; } if(answer==INF) answer=-1; printf("%d\n", answer); if(answer==-1) return; tr(it, ans) printf("%d ", *it); puts(""); } int main(){ // wow.pb(5, -1); // for(int mk = 0; mk < 32; ++mk) // cd[__builtin_popcount(mk)].pb(mk); int tt; scanf("%d", &tt); while(tt--) solve(); return 0; }

Compilation message (stderr)

balancedtree.cpp: In function 'void solve()':
balancedtree.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
balancedtree.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
balancedtree.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         scanf("%d", val+i), same[i]=val[i];
      |         ~~~~~^~~~~~~~~~~~~
balancedtree.cpp: In function 'int main()':
balancedtree.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |     scanf("%d", &tt);
      |     ~~~~~^~~~~~~~~~~
#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...