QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#521082#7151. Tree embeddingAllSolvedin1557WA 1ms5980kbC++173.3kb2024-08-15 21:02:192024-08-15 21:02:19

Judging History

你现在查看的是最新测评结果

  • [2024-08-15 21:02:19]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5980kb
  • [2024-08-15 21:02:19]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,ll> pii;

vector<pii>v[1005];

ll sz[1005],c0,ans,A[20][1005],n;
bool vis[1005];
ll dis[1005][1005];
map<pii,ll>mp[20],mp2[20];

void getdis(ll st)
{
    for(ll i=1;i<=n;i++) dis[st][i]=-1;
    dis[st][st]=0; queue<ll>q; q.push(st);
    while(!q.empty())
    {
        ll x=q.front(); q.pop();
        for(auto i:v[x])
        {
            if(dis[st][i.fi]!=-1) continue;
            dis[st][i.fi]=dis[st][x]+i.se;
            q.push(i.fi);
        }
    }
}

void getans(ll st)
{
    for(ll j=0;j<=ans;j++)
    {
        fill(vis+1,vis+1+n,0);
        vis[st]=1; queue<ll>q; q.push(st);
        while(!q.empty())
        {
            ll x=q.front(); q.pop();
            for(auto i:v[x])
            {
                if(vis[i.fi]) continue;
                ll c=mp2[j][{x,i.fi}];
                if(!vis[c]&&dis[1][c]==dis[1][i.fi]+dis[i.fi][c]) mp[j][{x,i.fi}]*=-1;
                A[j][i.fi]=A[j][x]+i.se*mp[j][{x,i.fi}];
                q.push(i.fi); vis[i.fi]=1;
            }
        }
    }
}
void init(ll x,ll p)
{
    sz[x]=1;
    for(auto i:v[x])
    {
        if(vis[i.fi]||i.fi==p) continue;
        init(i.fi,x);
        sz[x]+=sz[i.fi];
    }
}
ll get_cent(ll x,ll p,ll nn)
{
    for(auto i:v[x]) if(i.fi!=p&&!vis[i.fi]&&sz[i.fi]*2>nn) return get_cent(i.fi,x,nn);
    return x;
}
void dfs(ll x,ll p,ll t,ll b,ll c)
{
    if(p) mp[b][{x,p}]=mp[b][{p,x}]=t, mp2[b][{x,p}]=mp2[b][{p,x}]=c;
    for(auto i:v[x])
    {
        if(i.fi==p||vis[i.fi]) continue;
        dfs(i.fi,x,t,b,c);
    }
}
void f(ll x,ll b)
{
    init(x,0); ans=max(ans,b);
    ll nn=sz[x],c=get_cent(x,0,sz[x]);
    if(b==0) c0=c;
    if(nn==1) return;
    if(nn==2)
    {
        ll a;
        for(auto i:v[c]) if(!vis[i.fi]) a=i.fi;
        mp[b][{a,c}]=mp[b][{c,a}]=1;
        return;
    }
    init(c,0);
    vector<ll>v1,v2; ll cnt=0;
    vector<pii>tmp;
    for(auto i:v[c])
    {
        if(!vis[i.fi])
            tmp.push_back({sz[i.fi],i.fi});
    }
    sort(tmp.begin(),tmp.end());
    for(auto i:tmp)
    {
        if(i.fi+cnt<=nn/2) v1.push_back(i.se), cnt+=i.fi;
        else v2.push_back(i.se);
    }

    for(auto i:v1) vis[i]=1;
    dfs(c,0,1,b,c);
    f(c,b+1);
    for(auto i:v1) vis[i]=0;
    for(auto i:v2) vis[i]=1;
    dfs(c,0,-1,b,c);
    f(c,b+1);
    for(auto i:v2) vis[i]=0;
}


int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    for(ll i=1;i<n;i++)
    {
        ll a,b,c; cin>>a>>b>>c;
        v[a].push_back({b,c}); v[b].push_back({a,c});
    }
    for(ll i=1;i<=n;i++) getdis(i);
    f(1,0);
    cout<<ans+1<<'\n';
    getans(c0);
    for(ll j=1;j<=n;j++, cout<<'\n') for(ll i=0;i<=ans;i++) cout<<A[i][j]<<' ';
    /*for(ll i=1;i<=n;i++) for(ll j=i+1;j<=n;j++)
    {
        ll d=0;
        for(ll k=0;k<=ans;k++) d=max(d,abs(A[k][i]-A[k][j]));
        if(d!=dis[i][j])
        {
            cout<<i<<' '<<j<<'\n';
            for(ll k=0;k<=ans;k++) cout<<A[k][i]<<' ';
            cout<<'\n';
            for(ll k=0;k<=ans;k++) cout<<A[k][j]<<' ';
            cout<<'\n';
            cout<<dis[i][j]<<' '<<d<<'\n';
        }
    }*/
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3692kb

input:

2
1 2 2

output:

1
0 
2 

result:

ok 

Test #2:

score: 0
Accepted
time: 0ms
memory: 3872kb

input:

4
1 2 1
1 3 1
1 4 1

output:

3
0 0 0 
-1 -1 1 
-1 1 1 
1 1 0 

result:

ok 

Test #3:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

10
8 2 33305
2 6 69148
3 2 78693
5 9 4671
4 9 60174
7 2 53555
9 2 44205
1 5 51522
4 10 8094

output:

5
-56193 56193 -56193 -46851 0 
44205 -44205 -44205 44205 0 
122898 -122898 -122898 -34488 78693 
-60174 -60174 60174 60174 0 
-4671 4671 -4671 4671 0 
113353 -113353 -113353 113353 69148 
97760 -97760 9350 97760 0 
77510 -10900 -77510 77510 0 
0 0 0 0 0 
-68268 -68268 68268 68268 0 

result:

ok 

Test #4:

score: 0
Accepted
time: 0ms
memory: 3672kb

input:

7
3 2 84583
1 2 99813
2 6 69523
4 2 85328
5 7 95654
5 4 79707

output:

4
-99813 -99813 -99813 -99813 
0 0 0 0 
-84583 -84583 84583 84583 
85328 85328 85328 0 
165035 165035 165035 79707 
-69523 69523 69523 0 
260689 260689 260689 175361 

result:

ok 

Test #5:

score: 0
Accepted
time: 1ms
memory: 5704kb

input:

7
3 1 81630
4 3 90999
7 5 34787
4 2 45864
6 4 22160
3 7 70320

output:

4
-81630 -81630 -81630 0 
136863 45135 -136863 136863 
0 0 0 0 
90999 90999 -90999 90999 
-105107 105107 105107 105107 
113159 113159 -68839 90999 
-70320 70320 70320 70320 

result:

ok 

Test #6:

score: 0
Accepted
time: 1ms
memory: 5724kb

input:

9
5 9 52
7 9 97888
4 7 72858
1 6 71334
6 2 19443
8 2 31727
5 3 14365
2 7 75461

output:

4
166238 166238 -15316 23570 
75461 75461 75461 75461 
-112305 -112305 83471 112305 
-72858 72858 -72858 72858 
-97940 -97940 97836 97940 
94904 94904 56018 94904 
0 0 0 0 
107188 43734 107188 107188 
-97888 -97888 97888 97888 

result:

ok 

Test #7:

score: 0
Accepted
time: 0ms
memory: 3696kb

input:

2
1 2 20340

output:

1
0 
20340 

result:

ok 

Test #8:

score: 0
Accepted
time: 0ms
memory: 5624kb

input:

2
2 1 51883

output:

1
0 
51883 

result:

ok 

Test #9:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

8
8 7 10751
3 4 95845
6 7 62471
6 2 19671
2 3 29382
2 5 10022
7 1 3280

output:

4
-65751 59191 -65751 59191 
19671 19671 -19671 19671 
49053 49053 9711 49053 
144898 144898 105556 144898 
29693 9649 -29693 29693 
0 0 0 0 
-62471 62471 -62471 62471 
-73222 73222 -51720 62471 

result:

ok 

Test #10:

score: 0
Accepted
time: 1ms
memory: 3688kb

input:

9
4 9 36917
1 6 23007
5 4 46177
8 7 54587
1 7 71340
2 1 56998
6 3 27648
4 1 33264

output:

5
0 0 0 0 0 
-56998 -56998 56998 0 0 
-50655 50655 50655 50655 0 
33264 33264 33264 33264 33264 
79441 79441 -12913 79441 79441 
-23007 23007 23007 23007 0 
71340 -71340 71340 71340 0 
125927 -125927 125927 125927 0 
70181 70181 70181 70181 33264 

result:

ok 

Test #11:

score: 0
Accepted
time: 0ms
memory: 3724kb

input:

10
7 5 75030
7 6 72683
10 7 21009
1 10 98067
8 6 78011
2 6 37017
3 7 59811
7 9 75484
4 9 29913

output:

5
119076 -119076 -119076 -77058 0 
109700 109700 35666 -109700 109700 
-59811 -59811 -59811 59811 0 
-105397 105397 -105397 105397 0 
-75030 -75030 75030 75030 0 
72683 72683 72683 -72683 72683 
0 0 0 0 0 
150694 150694 150694 5328 72683 
-75484 75484 -75484 75484 0 
21009 -21009 -21009 21009 0 

result:

ok 

Test #12:

score: 0
Accepted
time: 0ms
memory: 3680kb

input:

9
6 4 45480
1 3 73208
8 5 48334
2 8 85607
9 3 26872
6 7 38398
6 8 56313
8 3 34234

output:

5
107442 -107442 -38974 -107442 -38974 
-85607 -85607 85607 0 0 
34234 -34234 34234 -34234 34234 
101793 101793 -101793 10833 45480 
-48334 48334 48334 0 0 
56313 56313 -56313 56313 0 
94711 94711 -94711 94711 38398 
0 0 0 0 0 
61106 -61106 61106 -7362 34234 

result:

ok 

Test #13:

score: 0
Accepted
time: 1ms
memory: 3920kb

input:

10
3 2 43219
1 2 36963
5 10 33305
6 9 69148
4 9 78693
2 9 4671
9 7 60174
10 9 53555
10 8 44205

output:

5
41634 -41634 -41634 -32292 -36963 
4671 -4671 -4671 4671 0 
47890 -47890 -47890 47890 43219 
-78693 -78693 -78693 78693 0 
86860 86860 -86860 20250 33305 
-69148 -69148 69148 69148 0 
-60174 60174 60174 0 0 
97760 97760 -97760 97760 44205 
0 0 0 0 0 
53555 53555 -53555 53555 0 

result:

ok 

Test #14:

score: 0
Accepted
time: 0ms
memory: 3784kb

input:

10
6 7 99813
1 9 69523
3 6 85328
9 5 95654
2 10 79707
2 8 98694
6 2 81188
6 9 62924
4 6 13856

output:

5
132447 132447 -132447 -6599 -69523 
81188 -81188 81188 81188 81188 
-85328 -85328 -85328 85328 0 
-13856 -13856 13856 13856 0 
158578 158578 -158578 158578 95654 
0 0 0 0 0 
-99813 99813 99813 0 0 
179882 -179882 -17506 179882 179882 
62924 62924 -62924 62924 0 
160895 -160895 160895 160895 81188 

result:

ok 

Test #15:

score: 0
Accepted
time: 0ms
memory: 3688kb

input:

10
1 2 90999
2 7 34787
2 6 45864
6 10 22160
6 4 70320
3 2 25420
2 9 2202
7 5 72292
8 7 40402

output:

5
-90999 -90999 -90999 -90999 0 
0 0 0 0 0 
-25420 -25420 25420 25420 0 
116184 -116184 -24456 116184 116184 
107079 107079 -37505 107079 107079 
45864 -45864 45864 45864 45864 
34787 34787 34787 34787 34787 
75189 75189 75189 75189 34787 
-2202 2202 2202 0 0 
68024 -68024 68024 68024 45864 

result:

ok 

Test #16:

score: 0
Accepted
time: 1ms
memory: 5980kb

input:

10
9 6 14889
9 10 52
5 9 97888
9 4 72858
9 7 71334
9 8 19443
9 1 31727
2 9 14365
9 3 75461

output:

5
-31727 -31727 -31727 -31727 -31727 
-14365 -14365 -14365 14365 14365 
-75461 -75461 75461 75461 0 
-72858 72858 -72858 72858 0 
-97888 97888 97888 97888 0 
14889 -14889 -14889 14889 0 
71334 -71334 71334 71334 0 
19443 19443 -19443 19443 0 
0 0 0 0 0 
52 52 52 52 0 

result:

ok 

Test #17:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

10
10 6 6075
10 2 98020
10 1 58423
2 4 99364
10 7 37755
8 7 13465
9 10 52741
3 7 91030
2 5 2008

output:

5
-58423 -58423 -58423 -58423 0 
98020 -98020 -98020 98020 0 
128785 128785 -128785 -53275 91030 
197384 -197384 -197384 -1344 99364 
100028 -100028 -100028 100028 2008 
-6075 -6075 6075 6075 0 
37755 37755 -37755 37755 0 
51220 51220 -51220 51220 13465 
-52741 52741 52741 0 0 
0 0 0 0 0 

result:

ok 

Test #18:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

10
7 6 86861
5 2 30580
8 6 10447
10 2 25869
2 6 62961
1 2 16000
3 4 6458
2 3 399
3 9 4363

output:

5
-16000 -16000 -16000 -16000 0 
0 0 0 0 0 
399 -399 399 399 399 
6857 -6857 -6059 6857 6857 
-30580 -30580 30580 30580 0 
62961 62961 62961 62961 62961 
149822 149822 -23900 149822 149822 
73408 73408 73408 73408 62961 
4762 -4762 4762 4762 399 
-25869 25869 25869 0 0 

result:

ok 

Test #19:

score: 0
Accepted
time: 1ms
memory: 3720kb

input:

10
10 3 10751
5 8 95845
1 10 62471
3 9 19671
8 4 29382
2 3 10022
7 1 3280
1 6 18279
10 8 30909

output:

5
-62471 -62471 -62471 0 0 
20773 -20773 -20773 729 10022 
10751 -10751 -10751 10751 0 
60291 60291 -60291 1527 29382 
126754 126754 -126754 126754 95845 
-80750 -80750 -80750 18279 0 
-65751 -65751 -59191 3280 0 
30909 30909 -30909 30909 0 
30422 -30422 -30422 30422 19671 
0 0 0 0 0 

result:

ok 

Test #20:

score: 0
Accepted
time: 0ms
memory: 3952kb

input:

10
3 9 1937
9 6 36917
10 4 23007
5 8 46177
10 2 54587
10 6 71340
7 9 56998
8 1 27648
6 8 33264

output:

5
-60912 -60912 5616 -27648 0 
125927 125927 -125927 16753 54587 
38854 -38854 34980 -38854 38854 
94347 94347 -94347 94347 23007 
-79441 -79441 79441 46177 0 
0 0 0 0 0 
93915 -93915 93915 20081 36917 
-33264 -33264 33264 0 0 
36917 -36917 36917 -36917 36917 
71340 71340 -71340 71340 0 

result:

ok 

Test #21:

score: 0
Accepted
time: 0ms
memory: 3988kb

input:

10
5 3 58531
5 8 2182
3 2 75030
7 4 72683
4 3 21009
4 1 98067
2 9 78011
2 10 37017
5 6 59811

output:

5
119076 -119076 -77058 -119076 -77058 
-75030 75030 75030 75030 0 
0 0 0 0 0 
21009 -21009 21009 -21009 21009 
58531 58531 58531 58531 58531 
118342 118342 -1280 118342 118342 
93692 -93692 93692 51674 21009 
60713 60713 60713 60713 58531 
-153041 -2981 153041 153041 0 
-112047 112047 112047 75030 0 

result:

ok 

Test #22:

score: 0
Accepted
time: 0ms
memory: 3752kb

input:

10
2 6 42660
5 6 45480
9 1 73208
3 4 48334
1 7 85607
4 8 26872
4 10 38398
6 4 56313
4 1 34234

output:

5
34234 -34234 34234 34234 -34234 
98973 98973 13653 -98973 98973 
-48334 -48334 -48334 48334 0 
0 0 0 0 0 
101793 101793 101793 -10833 56313 
56313 56313 56313 -56313 56313 
119841 -119841 -51373 119841 51373 
-26872 -26872 26872 26872 0 
107442 -107442 107442 107442 -34234 
-38398 38398 38398 0 0 

result:

ok 

Test #23:

score: 0
Accepted
time: 0ms
memory: 3936kb

input:

14
1 8 99172
5 14 84394
3 14 7875
8 11 46747
11 12 91464
1 10 41274
4 3 51473
12 9 99888
13 14 48591
6 3 42998
12 7 9735
2 11 62362
12 3 24208

output:

5
-237383 -237383 -54455 237383 -52425 
-153826 -153826 153826 29102 62362 
24208 24208 -24208 24208 0 
75681 -27265 -75681 -27265 51473 
116477 116477 -100727 116477 92269 
67206 -18790 -67206 67206 42998 
-9735 9735 -9735 -9735 9735 
-138211 -138211 44717 138211 46747 
-99888 99888 -99888 99888 99...

result:

ok 

Test #24:

score: 0
Accepted
time: 1ms
memory: 3824kb

input:

20
12 17 9662
8 18 79403
9 12 39209
12 2 88815
12 4 65532
13 11 55258
15 17 57286
17 10 31472
2 19 39326
20 8 53686
17 5 16764
2 16 79545
12 13 50718
1 14 58728
2 6 50181
20 2 7766
14 11 12309
3 13 18809
14 7 953

output:

6
177013 -177013 75577 -177013 59557 -3470 
88815 88815 88815 88815 -88815 88815 
69527 -69527 -69527 -69527 31909 18809 
-65532 65532 -65532 -65532 65532 0 
-26426 -26426 -7102 -7102 16764 0 
138996 138996 38634 38634 -138996 138996 
119238 -119238 17802 -119238 119238 56211 
150267 150267 150267 1...

result:

ok 

Test #25:

score: 0
Accepted
time: 0ms
memory: 3948kb

input:

30
3 5 28796
4 9 10034
22 12 89509
21 8 23277
10 2 3196
4 29 59345
6 20 11469
10 13 51654
10 4 71995
7 12 28072
3 23 52059
7 26 50022
4 6 93782
10 25 13380
16 3 23595
7 9 36250
9 15 83594
11 2 1106
18 24 24090
28 3 42510
27 28 96564
10 8 34047
2 19 97541
10 17 75663
10 3 40752
30 1 16408
24 7 93194
...

output:

8
138970 138970 138970 138970 57466 57466 27554 0 
-3196 3196 3196 3196 3196 3196 0 0 
40752 40752 40752 40752 -40752 -40752 40752 0 
71995 -71995 -71995 -71995 71995 71995 71995 0 
69548 69548 69548 11956 -69548 -69548 11956 28796 
165777 -165777 21787 21787 165777 71995 71995 0 
118279 -25711 -118...

result:

ok 

Test #26:

score: -100
Wrong Answer
time: 1ms
memory: 3892kb

input:

40
18 39 61594
12 17 36186
37 33 1532
10 6 91806
23 20 60906
19 9 15159
13 38 22702
22 3 60048
28 21 62359
22 25 6011
26 35 43190
34 31 4138
30 12 49058
25 6 89047
31 19 43124
13 4 49436
25 26 41836
25 5 16956
26 14 45706
21 36 68537
12 15 55029
33 27 52251
33 26 88024
34 21 78287
12 32 65664
12 25 ...

output:

8
141228 129206 129206 141228 -48120 -141228 -48120 0 
158805 146783 146783 158805 158805 -158805 158805 0 
66059 54037 54037 -54037 66059 -66059 66059 0 
194785 174695 -194785 -194785 -186509 -6915 194785 0 
-16956 -16956 16956 -16956 16956 0 0 0 
-89047 -89047 -89047 89047 89047 0 0 0 
-206817 -20...

result:

wrong answer T(3, 4) = 260844, L(3, 4) = 252568