Submission #167399

# Submission time Handle Problem Language Result Execution time Memory
167399 2019-12-08T07:53:44 Z theStaticMind Mag (COCI16_mag) C++14
120 / 120
709 ms 174328 KB
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
struct P{
      int mx,mn,B,S;
      void add(int X,int N){
            if(X>mx){
                  mn=mx;
                  mx=X;
                  S=B;
                  B=N;
            }
            else if(X>mn){
                  mn=X;
                  S=N;
            }
      }
      P(int a,int b){
            mx=a;
            mn=b;
            B=-1;
            S=-1;
      }
};
struct Rate{
      int pay,payda;
      long double get_value()const{
            return (long double)pay/(long double)payda;
      }
      bool operator<(const Rate& X)const{
            return get_value()<X.get_value();
      }
      Rate(int x,int y){
            pay=x;
            payda=y;
      }
};
vector<int>adj[1000005];
vector<int>val(1000005,INF);
vector<P> data(1000005,P(0,0));
void dfs(int x,int pre){
      for(int i=0;i<adj[x].size();i++){
            int y=adj[x][i];
            if(y==pre)continue;
            dfs(y,x);
            if(val[y]==1)
            data[x].add(data[y].mx+1,y);
      }
}
void push(int x,int pre){
      for(int i=0;i<adj[x].size();i++){
            int y=adj[x][i];
            if(y==pre)continue;
            if(val[x]==1){
                  if(data[x].B!=y)
                  data[y].add(data[x].mx+1,0);
                  else data[y].add(data[x].mn+1,0);
            }
            push(y,x);
      }
}
int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
   //   freopen("q.gir","r",stdin);
   //   freopen("q.cik","w",stdout);
      int n;
      cin>>n;
      for(int i=0;i<n-1;i++){
            int x,y;
            cin>>x>>y;
            adj[x].pb(y);
            adj[y].pb(x);
      }
      for(int i=1;i<=n;i++)cin>>val[i];
      if(*min_element(all(val))>1){
            cout<<*min_element(all(val))<<"/"<<1;
            return 0;
      }
      dfs(1,0);
      push(1,0);
      Rate ans(1,1);
      for(int x=1;x<=n;x++){
            if(val[x]==1){
                  ans=min(ans,Rate(1,data[x].mx+data[x].mn+1));
            }
            if(val[x]==2){
                  ans=min(ans,Rate(2,data[x].mx+data[x].mn+1));
            }
      }
      cout<<ans.pay/__gcd(ans.pay,ans.payda)<<"/"<<ans.payda/__gcd(ans.pay,ans.payda);
}

Compilation message

mag.cpp: In function 'void dfs(long long int, long long int)':
mag.cpp:49:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i=0;i<adj[x].size();i++){
                   ~^~~~~~~~~~~~~~
mag.cpp: In function 'void push(long long int, long long int)':
mag.cpp:58:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i=0;i<adj[x].size();i++){
                   ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 61 ms 62968 KB Output is correct
2 Correct 60 ms 62968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 62968 KB Output is correct
2 Correct 61 ms 63224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 500 ms 113088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 62968 KB Output is correct
2 Correct 615 ms 171384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 605 ms 154628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 528 ms 94112 KB Output is correct
2 Correct 435 ms 97356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 554 ms 96644 KB Output is correct
2 Correct 131 ms 68344 KB Output is correct
3 Correct 670 ms 174328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 66680 KB Output is correct
2 Correct 542 ms 94540 KB Output is correct
3 Correct 502 ms 89464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 525 ms 92908 KB Output is correct
2 Correct 709 ms 93812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 578 ms 94756 KB Output is correct
2 Correct 480 ms 81732 KB Output is correct