# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
720609 |
2023-04-08T15:34:21 Z |
Ahmed57 |
Mag (COCI16_mag) |
C++14 |
|
1008 ms |
176632 KB |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000006];
int ma[1000006];
int lon[1000006];
int pref[1000006];
int suf[1000006];vector<int> ch;
int n ;
void calc(int i,int pr){
int ch = (ma[i]==1?1:0);
for(auto j:adj[i]){
if(j!=pr){
calc(j,i);
if(ma[i]==1)ch = max(ch,lon[j]+1);
}
}
lon[i] = ch;
}
int maa = 0;
void dfs(int i,int pr){
maa = max(maa,lon[i]);
for(auto j:adj[i]){
if(j==pr)continue;
dfs(j,i);
}
if(ma[i]==1){
int firstma = 0 , secondma = 0;
for(auto j:adj[i]){
if(j==pr)continue;
if(firstma<=lon[j]){
secondma = firstma;
firstma = lon[j];
}else if(secondma<=lon[j]){
secondma = lon[j];
}
}
maa = max(maa,firstma+secondma+1);
}
}
bool ss = 0;int mm = 0;
void reroor(int i,int pr,int cnt){
if(ma[i]==2){
int firstma = cnt , secondma = 0;
for(auto j:adj[i]){
if(j==pr)continue;
if(firstma<=lon[j]){
secondma = firstma;
firstma = lon[j];
}else if(secondma<=lon[j]){
secondma = lon[j];
}
}
if(firstma==secondma&&firstma==maa){
ss = 1;
}
}
ch.clear();
mm = 0;
for(auto j:adj[i]){
if(j==pr)continue;
pref[j] = mm;
mm = max(mm,lon[j]);
ch.push_back(j);
}
mm = cnt;
reverse(ch.begin(),ch.end());
for(auto j:ch){
suf[j] = mm;
mm = max(mm,lon[j]);
}
for(auto j:adj[i]){
if(j==pr)continue;
reroor(j,i,(ma[i]==1?max(suf[j],pref[j])+1:0));
}
}
signed main(){
cin>>n;
for(int i = 0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int mi = 1e9;
for(int i = 0;i<n;i++){
cin>>ma[i+1];
mi = min(mi,ma[i+1]);
}
if(mi!=1){
cout<<mi<<"/"<<1<<endl;
return 0;
}
calc(1,0);
dfs(1,0);
cout<<"h"<<endl;
reroor(1,0,0);
if(ss) cout<<2<<"/"<<2*maa+1<<endl;
else cout<<1<<"/"<<maa<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
805 ms |
105156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1008 ms |
176632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
882 ms |
70492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
902 ms |
73008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
137 ms |
28572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
822 ms |
68856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
882 ms |
71444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |