QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#208622#2065. Cyclic Distancelzqy_WA 15ms7892kbC++141.5kb2023-10-09 19:26:102023-10-09 19:26:11

Judging History

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

  • [2023-10-09 19:26:11]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:7892kb
  • [2023-10-09 19:26:10]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define il inline
using namespace std;
const int maxn=200010;
il int read(){
    int x=0;
    char c=getchar();
    for(;!(c>='0'&&c<='9');c=getchar());
    for(;c>='0'&&c<='9';c=getchar())
        x=(x<<1)+(x<<3)+c-'0';
    return x;
}
struct edge{
    int v,w,to;
}e[maxn<<1];
int head[maxn],ecnt;
void addedge(int u,int v,int w){
    e[++ecnt].v=v,e[ecnt].w=w;
    e[ecnt].to=head[u],head[u]=ecnt;
}
ll pre[maxn];
int n,k,T,a[maxn],cnt;
bool vis[maxn];
void dfs(int fa,int x,int lw=0){
    if(vis[x]&&fa) return ;
    pre[x]=pre[fa]+lw;
    for(int i=head[x];i;i=e[i].to)
        if(e[i].v^fa) dfs(x,e[i].v,e[i].w);
}
int ldis(int x){
    for(int i=1;i<=n;i++) pre[i]=0;
    dfs(0,x);
    ll Mx=0,id;
    for(int i=1;i<=n;i++)
        if(pre[i]>Mx) Mx=pre[i],id=i;
    return id;
}
int main(){
    T=read();
    while(T--){
        for(int i=1;i<=n;i++) head[i]=vis[i]=0; ecnt=cnt=0;
        n=read(),k=read();
        for(int i=1;i<n;i++){
            int x=read(),y=read(),z=read();
            addedge(x,y,z),addedge(y,x,z);
        }
        ll sum=0,Mx=0,num;
        a[1]=ldis(1),vis[a[1]]=1;
        for(int i=2;i<k;i++)
            a[i]=ldis(a[i-1]),vis[a[i]]=1,sum+=pre[a[i]];
        for(int i=1;i<=n;i++)
            if(!vis[i]){
                for(int j=1;j<=n;j++) pre[j]=0;
                dfs(0,a[1]),num=pre[i];
                dfs(0,a[k-1]),num+=pre[i];
                Mx=max(Mx,num);
            }
        printf("%lld\n",sum+Mx);
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 7876kb

input:

1
5 3
1 2 4
1 3 1
1 4 8
4 5 9

output:

44

result:

ok single line: '44'

Test #2:

score: -100
Wrong Answer
time: 15ms
memory: 7892kb

input:

57206
3 2
1 2 574927
2 3 406566
2 2
1 2 308806
4 3
1 2 312588
2 3 500141
2 4 53602
3 3
1 2 797183
2 3 944061
5 3
1 2 624010
2 3 488613
3 4 734514
4 5 497493
5 4
1 2 540278
2 3 488655
2 4 228989
2 5 653896
2 2
1 2 569117
4 2
1 2 821764
2 3 499471
1 4 549060
2 2
1 2 991159
2 2
1 2 482941
5 4
1 2 30462...

output:

1962986
617612
1732662
3482488
4689260
3823636
1138234
3740590
1982318
965882
3418504
5026562
1623414
1885106
1952142
3050630
1691896
3102076
2380932
3076270
4426605
5281467
879020
2500212
3613854
1358950
1182198
2273662
2331560
1681964
7532773
2373066
3163042
3104226
3642898
2105502
3886524
3669186...

result:

wrong answer 21st lines differ - expected: '5697196', found: '4426605'