/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
int n;
int k;
int minimum [70010];
vector<int> adjl[70010];
int vis[70010];
int level[70010];
int parent[70010];
int jump(int x){
return parent[x];
}
int lca(int x, int y){
if(level[x]<level[y]){
swap(x,y);
}
int tren = x;
while(level[tren] != level[y]){
tren = jump(tren);
}
cout<<tren<<endl;
while(tren != y){
tren = jump(tren);
y= jump(y);
}
return tren;
}
void dfs(int s){
vis[s]++;
for(int i = 0; i <adjl[s].size();i++){
if(vis[adjl[s][i]]==0){
level[adjl[s][i]]=level[s]+1;
parent[adjl[s][i]]=s;
dfs(adjl[s][i]);
}
}
}
map<pair<int,int>,int>mapa;
int main()
{
parent[1] = -1;
cin >> n;
int x1[n],y1[n];
for(int i = 0; i < (n-1); i++){
cin >> x1[i] >> y1[i];
int x = x1[i];
int y = y1[i];
mapa[{x,y}] = -1;
mapa[{y,x}]=-1;
adjl[x].push_back(y);
adjl[y].push_back(x);
}
dfs(1);
// cout<<level[1]<<" "<<level[2]<<endl;
//cout<<lca(1,2)<<endl;
cin >> k;
vector<pair<int,pair<int,pair<int,int> > > >v;
while(k--){
char c;
cin >> c;
int x, y,z;
cin>>x>>y>>z;
int dist = level[x] + level[y] - 2*level[lca(x,y)];
v.push_back({dist,{z,{x,y}}});
}
cout<<1<<endl;
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
for(int i = 0; i < v.size(); i++){
int c = v[i].second.first;
int prvi = v[i].second.second.first;
int drugi = v[i].second.second.second;
int zajednicki = lca(prvi,drugi);
int tren = prvi;
int used = 0;
while(tren != zajednicki){
if(mapa[{tren,parent[tren]}] == -1){
mapa[{tren,parent[tren]}] = c;
mapa[{parent[tren],tren}] = c;
used ++;
}else{
if(used == 0){
mapa[{tren,parent[tren]}] = c;
mapa[{parent[tren],tren}] = c;
used ++;
}
}
tren = parent[tren];
}
tren = drugi;
while(tren != zajednicki){
if(mapa[{tren,parent[tren]}] == -1){
mapa[{tren,parent[tren]}] = c;
mapa[{parent[tren],tren}] = c;
used ++;
}else{
if(used == 0){
mapa[{tren,parent[tren]}] = c;
mapa[{parent[tren],tren}] = c;
used ++;
}
}
tren = parent[tren];
}
}
for (int i = 0; i<(n-1);i++){
if(mapa[{x1[i],y1[i]}]==-1){
mapa[{x1[i],y1[i]}] = 0;
}
cout<<x1[i]<<" "<<y1[i]<<" "<<mapa[{x1[i],y1[i]}]<<endl;
}
}
Compilation message
minmaxtree.cpp: In function 'void dfs(int)':
minmaxtree.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i <adjl[s].size();i++){
| ~~^~~~~~~~~~~~~~~
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, std::pair<int, int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
not a valid edge |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
907 ms |
22608 KB |
not a valid edge |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
406 ms |
17724 KB |
not a valid edge |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
not a valid edge |
2 |
Halted |
0 ms |
0 KB |
- |