#include <bits/stdc++.h>
#define DIM 700010
using namespace std;
int level[DIM],fth[DIM];
int E[DIM*4],p[DIM*4],first[DIM],viz[DIM],tata[DIM],dp_min[DIM];
pair <int,int> rmq[30][DIM];
vector <int> L[DIM];
struct _update{
int tip,x,y,val;
};
_update v[DIM],w[DIM];
int n,m,i,j,x,y,val,dist,lca,k,tip,el;
char ch;
void dfs (int nod, int father){
E[++k] = nod;
first[nod] = k;
level[nod] = 1 + level[father];
fth[nod] = tata[nod] = father;
for (int i=0;i<L[nod].size();i++){
int vecin = L[nod][i];
if (vecin != father){
dfs (vecin,nod);
E[++k] = nod;
}}}
int get_lca (int x, int y){
int posx = first[x], posy = first[y];
if (posx > posy)
swap (posx,posy);
int nr = p[posy-posx+1];
pair <int,int> sol = min (rmq[nr][posx],rmq[nr][posy-(1<<nr)+1]);
return E[sol.second];
}
inline int cmp (_update a, _update b){
return a.val < b.val;
}
int main (){
// ifstream cin ("minmaxtree.in");
// ofstream cout ("minmaxtree.out");
cin>>n;
for (i=1;i<n;i++){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
dfs (1,0);
for (i=1;i<=k;i++)
rmq[0][i] = make_pair (level[E[i]],i);
for (i=1;(1<<i)<=k;i++){
for (j=1;j<=k;j++){
rmq[i][j] = rmq[i-1][j];
if (j + (1<<(i-1)) <= k && rmq[i-1][j + (1<<(i-1))].first < rmq[i][j].first)
rmq[i][j] = rmq[i-1][j + (1<<(i-1))];
}}
for (i=2;i<=k;i++)
p[i] = p[i/2] + 1;
cin>>m;
for (i=1;i<=m;i++){
cin>>ch>>x>>y>>val;
if (ch == 'M')
tip = 1;
else tip = 0;
v[i] = {tip,x,y,val};
if (tip)
w[++el] = v[i];
}
/// dp_min[nod] - minimul valorilor de maxim care trec prin muchia nod-fth[nod]
sort (w+1,w+el+1,cmp);
for (i=1;i<=el;i++){
int x = w[i].x, y = w[i].y, val = w[i].val;
lca = get_lca (x,y);
/// acum incerc sa fac toate valorile nemcacate de pe drumul de la x la lca egale cu val
int nod = x;
while (level[nod] > level[lca]){
if (!dp_min[nod]){
dp_min[nod] = val;
for (auto vecin : L[nod]){
if (vecin != tata[nod])
fth[vecin] = lca;
}
}
nod = fth[nod];
}
nod = y;
while (level[nod] > level[lca]){
if (!dp_min[nod]){
dp_min[nod] = val;
for (auto vecin : L[nod]){
if (vecin != tata[nod])
fth[vecin] = lca;
}
}
nod = fth[nod];
}
}
for (i=2;i<=n;i++)
cout<<i<<" "<<tata[i]<<" "<<dp_min[i]<<"\n";
return 0;
}
Compilation message
minmaxtree.cpp: In function 'void dfs(int, int)':
minmaxtree.cpp:21:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<L[nod].size();i++){
~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
16888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
46932 KB |
Output is correct |
2 |
Correct |
249 ms |
46840 KB |
Output is correct |
3 |
Execution timed out |
1062 ms |
46200 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
165 ms |
42744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
16888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |