QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#472845 | #904. 最小树形图 | Bronya# | AC ✓ | 63ms | 38204kb | C++14 | 2.8kb | 2024-07-11 19:50:54 | 2024-07-11 19:50:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m,r;
struct edge{
int u,v,w;
}e[400005];
struct Heap{
int lson,rson;
int len;
long long val,tag;
int fr,id;
}t[400005];
int root[400005];
int g;
int Newnode(long long val,int fr,int id){
t[++g]={0,0,1,val,0,fr,id};
return g;
}
void pu(int rt,long long val){
if(!rt)return;
t[rt].tag+=val;
t[rt].val+=val;
}
void pushdown(int rt){
if(t[rt].tag==0)return;
pu(t[rt].lson,t[rt].tag);
pu(t[rt].rson,t[rt].tag);
t[rt].tag=0;
}
void merge(int &rt,int x,int y){
if(!x||!y){
rt=x+y;
return;
}
if(t[x].val>t[y].val)swap(x,y);
rt=x;pushdown(x);
merge(t[rt].rson,t[x].rson,y);
if(t[t[rt].rson].len>t[t[rt].lson].len)swap(t[rt].lson,t[rt].rson);
t[rt].len=t[t[rt].rson].len+1;
}
long long ans=0;
int fa[400005],fa2[400005],cnt;
bool pd[400005];
int Find(int u){
return (fa[u]==u?u:(fa[u]=Find(fa[u])));
}
int Find2(int u){
return (fa2[u]==u?u:(fa2[u]=Find2(fa2[u])));
}
stack<int>st;
int Fa[400005];
int Tarjan(int u){
cnt++;
while(!st.empty()){
int v=st.top();st.pop();
merge(root[cnt],root[cnt],root[v]);
fa2[v]=cnt;Fa[v]=cnt;
if(v==u)break;
}
if(!st.empty()){
int f1=Find(st.top());
fa[cnt]=f1;
}
return cnt;
}
int fr[400005];
int lans[400005];
vector<int>son[200005];
void Solve(int u){
if(pd[u])return;
st.push(u);pd[u]=true;
int v=Find2(t[root[u]].fr);
while(u==v){
pushdown(root[u]);
merge(root[u],t[root[u]].lson,t[root[u]].rson);
v=Find2(t[root[u]].fr);
}
fr[u]=t[root[u]].id;
long long d=t[root[u]].val;
ans+=d;pushdown(root[u]);
merge(root[u],t[root[u]].lson,t[root[u]].rson);
pu(root[u],-d);
int f1=Find(u),f2=Find(v);
if(f1!=f2){
fa[f1]=f2;
Solve(v);
}
else Solve(Tarjan(v));
}
bool chk[200005];
void del(int u){
if(chk[u])return;
chk[u]=true;del(Fa[u]);
}
int main(){
scanf("%d%d%d",&n,&m,&r);r++;
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);u++;v++;e[i]={u,v,w};
merge(root[v],root[v],Newnode(w,u,i));
}
for(int i=2;i<=n;i++)
merge(root[i],root[i],Newnode(1e18,i-1,0));
merge(root[1],root[1],Newnode(1e18,n,0));
for(int i=1;i<=n*2;i++)fa[i]=i,fa2[i]=i,Fa[i]=i;pd[r]=true;
cnt=n;
for(int i=1;i<=n;i++){
while(!st.empty())st.pop();
Solve(i);
}
printf("%lld\n",ans);
for(int i=cnt;i>=1;i--){
if(!chk[i]&&i!=r){
lans[e[fr[i]].v]=e[fr[i]].u;
del(e[fr[i]].v);
}
}
lans[r]=r;
for(int i=1;i<=n;i++)printf("%d ",lans[i]-1);
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18468kb
input:
4 4 0 0 1 10 0 2 10 0 3 3 3 2 4
output:
17 0 0 3 0
result:
ok OK: 17
Test #2:
score: 0
Accepted
time: 3ms
memory: 19264kb
input:
7 8 3 3 1 10 1 2 1 2 0 1 0 1 1 2 6 10 6 4 1 4 5 1 5 6 1
output:
24 2 3 1 3 6 4 2
result:
ok OK: 24
Test #3:
score: 0
Accepted
time: 55ms
memory: 36024kb
input:
199952 199993 133795 133795 102355 1000000000 133795 83671 1000000000 102355 154742 1000000000 83671 146772 1000000000 133795 20306 1000000000 102355 22952 1000000000 20306 55446 1000000000 146772 158455 1000000000 20306 150512 1000000000 158455 147410 1000000000 133795 128604 1000000000 20306 11322...
output:
199931189898972 128957 34797 132122 134056 189307 143877 84285 76338 189301 173315 179171 74929 148937 35695 116683 160721 130059 92784 43637 199577 87882 866 91007 40762 13659 152307 173085 159119 25500 161172 71194 89702 19323 153725 36336 55215 101904 193100 62562 101869 40261 113649 15341 5363 1...
result:
ok OK: 199931189898972
Test #4:
score: 0
Accepted
time: 57ms
memory: 36080kb
input:
199969 199988 132538 132538 133306 1000000000 133306 65647 1000000000 65647 150927 1000000000 65647 164729 1000000000 133306 23666 1000000000 132538 18547 1000000000 133306 150037 1000000000 18547 25427 1000000000 133306 123705 1000000000 23666 173397 1000000000 150037 192082 1000000000 23666 73257 ...
output:
199957566159091 18880 68793 140633 99419 66029 44671 142672 90144 74569 117222 152493 134760 40752 160499 84035 67665 124116 198582 24554 112879 130809 23236 23398 175520 97922 83460 137793 189194 194955 62183 114534 8317 132525 185581 29788 191645 58118 97298 133954 32740 30570 78064 171080 19449 4...
result:
ok OK: 199957566159091
Test #5:
score: 0
Accepted
time: 54ms
memory: 36144kb
input:
199987 199996 73393 73393 73643 1000000000 73643 95893 1000000000 73393 5600 1000000000 73393 158621 1000000000 73393 6514 1000000000 73643 66541 1000000000 95893 117271 1000000000 95893 81406 1000000000 117271 66016 1000000000 117271 141093 1000000000 66016 124816 1000000000 6514 169925 1000000000 ...
output:
199981770027014 66109 29328 46922 170491 70446 125979 129506 81931 78901 72407 63308 184071 11462 27054 179467 17412 41270 183516 42793 165139 99508 134784 122383 90735 121367 91341 93208 187959 193990 120257 86583 59377 61694 164390 168801 122038 102174 31754 30221 18089 44679 120616 180352 44896 1...
result:
ok OK: 199981770027014
Test #6:
score: 0
Accepted
time: 63ms
memory: 38204kb
input:
199964 199980 160906 160906 164092 1000000000 160906 162309 1000000000 162309 171340 1000000000 162309 17527 1000000000 164092 134343 1000000000 17527 41420 1000000000 17527 151665 1000000000 164092 112863 1000000000 112863 81241 1000000000 164092 35555 1000000000 41420 8818 1000000000 171340 140799...
output:
199955963101743 152707 52653 181074 54759 157790 75974 49275 13068 119732 112709 174491 89036 13777 158335 22 5356 114823 34717 160294 191040 131268 189741 129105 121548 196952 156697 13554 161430 135531 78389 20705 56030 62749 76935 161439 41771 192995 59944 97896 118159 119787 175398 167727 32759 ...
result:
ok OK: 199955963101743
Test #7:
score: 0
Accepted
time: 54ms
memory: 38108kb
input:
199919 199962 23431 23431 96415 1000000000 23431 191925 1000000000 191925 9560 1000000000 9560 170529 1000000000 170529 159881 1000000000 191925 120224 1000000000 96415 6680 1000000000 9560 193433 1000000000 9560 85664 1000000000 6680 181717 1000000000 170529 158939 1000000000 191925 107250 10000000...
output:
199897349440162 103186 93718 180920 167245 9287 186702 6264 110196 129062 145614 71631 87602 159726 18018 142454 18435 193668 93488 162246 52925 194363 125276 120272 107605 163894 24400 64442 139909 164378 180241 12903 184506 174699 174619 180410 188211 22000 170255 68547 121411 153403 82617 199859 ...
result:
ok OK: 199897349440162
Test #8:
score: 0
Accepted
time: 43ms
memory: 33060kb
input:
127669 145374 45792 45792 71618 1000000000 45792 115885 1000000000 71618 49565 1000000000 115885 111574 1000000000 45792 20265 1000000000 71618 14268 1000000000 20265 94531 1000000000 111574 41288 1000000000 20265 105551 1000000000 41288 56819 1000000000 45792 71896 1000000000 20265 9947 1000000000 ...
output:
119282622630242 34046 27516 51025 54831 22263 121267 66184 100275 11263 8114 121337 87469 14363 122204 3034 79382 15843 18508 26561 4550 42198 100867 89741 118228 2294 78758 87028 92838 3212 72627 14909 42905 46828 94702 107137 79579 19155 38912 16582 54824 11947 93970 117681 73750 62191 35845 86407...
result:
ok OK: 119282622630242
Test #9:
score: 0
Accepted
time: 47ms
memory: 35684kb
input:
150763 168446 127700 127700 129217 1000000000 129217 59686 1000000000 59686 147340 1000000000 59686 83033 1000000000 129217 19448 1000000000 127700 63595 1000000000 129217 146469 1000000000 63595 20895 1000000000 129217 117799 1000000000 19448 14887 1000000000 146469 144756 1000000000 19448 69749 10...
output:
142247087752207 141373 124034 104637 81757 105355 84746 12198 105974 74556 118129 137094 52587 56448 103751 51728 62771 27939 99036 94042 132717 38126 106577 17353 57731 45064 99632 115422 31017 56808 124156 25322 105446 117953 17255 54833 70676 143873 90561 51122 86115 17983 67981 51488 48 26321 10...
result:
ok OK: 142247087752207
Test #10:
score: 0
Accepted
time: 38ms
memory: 31484kb
input:
53336 173537 38785 38785 12889 1000000000 12889 7127 1000000000 38785 7453 1000000000 38785 34343 1000000000 38785 32477 1000000000 12889 13636 1000000000 7127 15597 1000000000 7127 13103 1000000000 15597 47076 1000000000 15597 148 1000000000 47076 1956 1000000000 32477 28402 1000000000 34343 3303 1...
output:
21192431283643 50094 3936 12931 22228 8601 49651 35230 35301 6028 9217 31386 47739 34246 11070 48457 21051 38379 25654 28682 34634 10408 15702 44178 32555 22101 49237 39915 49792 7259 26298 22347 28492 24422 19071 18969 52271 27299 47043 46994 32786 431 25409 32782 25205 11393 23766 32902 33835 4681...
result:
ok OK: 21192431283643
Test #11:
score: 0
Accepted
time: 59ms
memory: 37368kb
input:
167105 192942 123259 123259 128127 1000000000 123259 165809 1000000000 165809 135383 1000000000 135383 118054 1000000000 128127 85861 1000000000 118054 17299 1000000000 118054 109533 1000000000 128127 164356 1000000000 164356 125823 1000000000 128127 113752 1000000000 17299 9958 1000000000 135383 94...
output:
154804117296498 156441 99386 39829 22949 119179 166797 152236 101587 139595 54295 145353 43760 101477 130938 101608 141817 35195 161505 157109 88774 162839 57379 120894 13612 38728 40807 148757 135752 5969 140992 135311 30685 160102 128812 45653 157651 11759 14628 91765 5977 55257 144054 155333 8629...
result:
ok OK: 154804117296498
Test #12:
score: 0
Accepted
time: 31ms
memory: 26624kb
input:
14868 113983 9831 9831 2529 1000000000 9831 4064 1000000000 4064 2371 1000000000 2371 8498 1000000000 8498 2163 1000000000 4064 11015 1000000000 2529 6953 1000000000 2371 14269 1000000000 2371 2171 1000000000 6953 4073 1000000000 8498 1004 1000000000 4064 5444 1000000000 4073 576 1000000000 6953 110...
output:
2199325501507 136 6652 8116 4340 4025 13013 11786 2874 5257 10401 12846 6494 5382 8594 8147 14080 5874 4330 3052 716 6538 14041 12070 370 1059 2680 14096 4986 14080 1266 7755 12033 10759 7620 4904 536 475 2451 10942 14748 4219 2943 13173 12322 11841 7891 1728 10770 2795 12597 11521 2907 14617 2144 3...
result:
ok OK: 2199325501507