Submission #913448

#TimeUsernameProblemLanguageResultExecution timeMemory
913448MilosMilutinovicHard route (IZhO17_road)C++14
52 / 100
2045 ms33476 KiB
#include<bits/stdc++.h> #define pb push_back #define fi first #define se second #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef long double ld; template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;} template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;} ll readint(){ ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,tot; int v[1000005],nxt[1000005],h[500005],mx[500005],cnt[500005]; void addedge(int x,int y){ v[++tot]=y; nxt[tot]=h[x]; h[x]=tot; v[++tot]=x; nxt[tot]=h[y]; h[y]=tot; } void dfs(int x,int px,int d){ mx[x]=d; cnt[x]=1; for(int p=h[x];p;p=nxt[p]){ if(v[p]==px) continue; dfs(v[p],x,d+1); if(mx[v[p]]==mx[x]) cnt[x]+=cnt[v[p]]; else if(mx[x]<mx[v[p]]) cnt[x]=cnt[v[p]]; mx[x]=max(mx[x],mx[v[p]]); } } int main(){ n=readint(); for(int i=1;i<n;i++) addedge(readint(),readint()); ll res=0,c=1; for(int i=1;i<=n;i++){ dfs(i,i,0); vector<pii> f; for(int p=h[i];p;p=nxt[p]) f.pb(mp(mx[v[p]],cnt[v[p]])); sort(f.rbegin(),f.rend()); if((int)f.size()<=2) continue; ll cur=0,cc=0; cur=f[0].fi*(f[1].fi+f[2].fi); ll s=0; for(int j=0;j<f.size();j++){ if(f[j].fi==f[2].fi) cc+=s*f[2].se; if(f[j].fi==f[1].fi) s+=f[j].se; } if(res<cur) res=cur,c=cc; else if(res==cur) c+=cc; } printf("%lld %lld\n",res,c); return 0; }

Compilation message (stderr)

road.cpp: In function 'int main()':
road.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for(int j=0;j<f.size();j++){
      |               ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...