# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
370064 |
2021-02-23T07:12:01 Z |
maozkurt |
Mag (COCI16_mag) |
C++17 |
|
4000 ms |
82104 KB |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <numeric>
#include <cassert>
#define endl '\n'
#define sp ' '
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn = 1e6+5;
const int inf = 2e9;
vector<int> adj[maxn];
int magic[maxn];
int p[maxn];
int dfs1(int v, int s){
int ret = v;
for(int i : adj[v]){
if(magic[i] == 1 && p[i] != s){
p[i] = s;
ret = dfs1(i,s);
}
}
return ret;
}
int dfs2(int v, int s){
int ret = 1;
for(int i : adj[v]){
if(magic[i] == 1 && p[i] != s){
p[i] = s;
ret = max(ret, dfs2(i,s) + 1);
}
}
return ret;
}
int dfs3(int s){
int maxx = -1, iki = -1;
for(int i : adj[s]){
if(magic[i] == 1){
p[i] = s;
int cur = dfs2(i,s);
if(cur>maxx){
iki = maxx;
maxx = cur;
}
else if(cur>iki){
iki = cur;
}
}
}
return (iki==-1 ? -1 : maxx + iki + 1);
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cerr.tie(nullptr);
int n;cin>>n;
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
int minn = inf;
for(int i=1;i<=n;i++){
cin>>magic[i];
minn = min(minn,magic[i]);
}
if(minn > 1){
cout << minn << '/' << 1 << endl;
exit(0);
}
int maxbir = 0;
for(int i=1;i<=n;i++){
if(magic[i]==1 && p[i] == 0){
p[i] = i;
int cur = dfs1(i,i);
if(cur==i){
maxbir = max(maxbir,1);
}
else{
p[cur] = cur;
maxbir = dfs2(cur,cur);
}
}
}
int maxiki = 0;
for(int i=1;i<=n;i++){
if(magic[i] == 2){
int cur = dfs3(i);
maxiki = max(maxiki, cur);
}
}
if(2*maxbir >= maxiki){
cout << 1 << '/' << maxbir;
}
else{
int sol = 2;
if(maxiki % 2 == 0){
sol=1;
maxiki/=2;
}
cout << sol << '/' << maxiki << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Incorrect |
17 ms |
24044 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
394 ms |
82104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23788 KB |
Output is correct |
2 |
Incorrect |
387 ms |
64492 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
440 ms |
63340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
64620 KB |
Output is correct |
2 |
Incorrect |
303 ms |
54380 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
372 ms |
63952 KB |
Output is correct |
2 |
Incorrect |
397 ms |
29496 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4026 ms |
29164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
355 ms |
62308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1139 ms |
65132 KB |
Output is correct |
2 |
Incorrect |
404 ms |
46408 KB |
Output isn't correct |