QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#310475#5548. Increase the Toll FeesZIhan#WA 60ms52624kbC++203.1kb2024-01-21 14:37:302024-01-21 14:37:30

Judging History

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

  • [2024-01-21 14:37:30]
  • 评测
  • 测评结果:WA
  • 用时:60ms
  • 内存:52624kb
  • [2024-01-21 14:37:30]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define I_am_weak ios::sync_with_stdio(0),cin.tie(0);
using namespace std;
int n,m;
int anc[20][100005];
int mx[20][100005];
int f[100005];
int find(int x){
    if(f[x]==x)return x;
    else return f[x]=find(f[x]);
}
struct ff{
	int x,y;
	int v;
};
bool cmp(ff a,ff b){
    return a.v<b.v;
}
vector<ff>edge,mst,last;
vector<pii>g[100005];
int in[100005],out[100005];
bool ch(int x,int y){
    return in[x]<=in[y]&&out[x]>=out[y];
}
int getlca(int x,int y){
    if(ch(x,y))return x;
    if(ch(y,x))return y;
    for(int i=19;i>=0;i--){
        if(ch(anc[i][x],y)==0){
            x=anc[i][x];
        }
    }
    return anc[0][x];
}
bool vis[100005];
int owowowowo=1;
void dfs1(int now,int pre){
    vis[now]=1;
    in[now]=++owowowowo;
    anc[0][now]=pre;
    for(auto [nxt,val]:g[now]){
        if(nxt==pre)continue;
        mx[0][nxt]=val;
        anc[0][nxt]=now;
        //cout<<now<<" "<<nxt<<" "<<val<<endl;
        dfs1(nxt,now);
    }
    out[now]=owowowowo;
}
signed main(){
	//注意多筆測資輸入!!!
	I_am_weak
	cin>>n>>m;
	for(int i=0;i<m;i++){
		ff now;
		cin>>now.x>>now.y>>now.v;
		edge.push_back(now);
	}
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    sort(edge.begin(),edge.end(),cmp);
    for(int i=0;i<m;i++){
        ff now=edge[i];
        int x=find(now.x);
        int y=find(now.y);
        if(x==y){
            last.push_back(now);
            continue;
        }else {
            mst.push_back(now);
            f[x]=y;
        }
    }
    int res=find(f[1]);
    for(int i=1;i<=n;i++){
        if(res!=find(i)){
            cout<<-1<<endl;
            return 0;
        }
    }
    sort(last.begin(),last.end(),cmp);
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    for(auto now:last){
        int x=find(now.x);
        int y=find(now.y);
        if(x==y){
            continue;
        }else {
            f[x]=y;
            g[now.x].push_back({now.y,now.v});
            g[now.y].push_back({now.x,now.v});
        }
    }
    dfs1(1,1);
    for(int i=1;i<=19;i++){
        for(int j=1;j<=n;j++){
            anc[i][j]=anc[i-1][anc[i-1][j]];
            mx[i][j]=mx[i-1][anc[i-1][j]];
        }
    }
    for(int i=1;i<=n;i++){
        if(vis[i]==0){
            cout<<-1<<endl;
            return 0;
        }
    }
    int ans=0;
    for(auto [x,y,vv]:mst){
        int lca=getlca(x,y);
        int mxx=0;
        //cout<<x<<" "<<y<<":"<<lca<<":";
        if(x!=lca){
            for(int i=19;i>=0;i--){
                if(ch(anc[i][x],lca)==0){
                    mxx=max(mxx,mx[i][x]);
                    x=anc[i][x];
                }
            }
            mxx=max(mxx,mx[0][x]);
        }
        if(y!=lca){
            for(int i=19;i>=0;i--){
                if(ch(anc[i][y],lca)==0){
                    mxx=max(mxx,mx[i][y]);
                    y=anc[i][y];
                }
            }
            
            mxx=max(mxx,mx[0][y]);
        }
        ans+=mxx-vv+1;
    }
    cout<<ans<<endl;
}

詳細信息

Test #1:

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

input:

4 6
1 2 2
1 3 5
1 4 5
2 3 3
2 4 5
3 4 4

output:

9

result:

ok single line: '9'

Test #2:

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

input:

3 4
1 2 3
2 3 4
1 3 5
1 3 10

output:

-1

result:

ok single line: '-1'

Test #3:

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

input:

5 10
1 2 14
1 3 14
1 4 9
1 5 15
2 3 8
2 3 10
2 4 13
3 4 8
4 5 10
4 5 15

output:

21

result:

ok single line: '21'

Test #4:

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

input:

2 1
1 2 1

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 49ms
memory: 42972kb

input:

29171 100000
7223 21138 270743473
5598 27967 847631606
12666 26050 847631606
75 15747 270743473
8522 12955 847631606
6069 23750 270743473
18708 22605 847631606
16814 22169 847631606
11550 27119 847631606
562 15959 847631606
9400 11110 270743473
15325 23805 270743473
19169 24404 270743473
6649 12062 ...

output:

16827826868780

result:

ok single line: '16827826868780'

Test #6:

score: 0
Accepted
time: 60ms
memory: 52624kb

input:

47977 200000
10970 47321 440845807
1166 29708 767952745
319 37520 546280762
17581 29425 558321466
22079 26884 344816304
7479 44260 791002634
14685 44163 837529020
1537 10359 330017953
8634 27064 969738917
32950 37192 728271930
34751 42782 63025978
32540 34226 86057211
36786 46050 826927874
30444 436...

output:

-1

result:

ok single line: '-1'

Test #7:

score: -100
Wrong Answer
time: 32ms
memory: 38980kb

input:

28825 57648
9446 22014 286256842
14902 20222 14175
3246 20218 80493268
1783 13768 931622563
11107 24862 918832025
25070 27312 98899079
8535 20222 16037
9184 17491 294248461
8799 17834 456827944
1152 11687 960740527
17849 23045 9706
5774 21436 444202963
5417 23045 3092
20222 20370 11232
16585 20222 1...

output:

27475531040329

result:

wrong answer 1st lines differ - expected: '28822649262260', found: '27475531040329'