QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#199962 | #7343. Bicycle Race | Vengeful_Spirit# | RE | 0ms | 0kb | C++14 | 2.7kb | 2023-10-04 14:49:14 | 2023-10-04 14:49:14 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=200001;
int ind[N];
pair<int,int>tag[N];
vector<pair<int,int>>e[N];
vector<tuple<int,int,int>>res[N];
int ord(int x,int v){
return (ind[x]>ind[v]||(ind[x]==ind[v]&&x>v));
}
void report(int x,int y,int z,int val){
// cout<<x<<" "<<y<<" "<<z<<" "<<val<<endl;
if(y>z)res[x].push_back({y,z,val});
else res[x].push_back({z,y,val});
if(x>z)res[y].push_back({x,z,val});
else res[y].push_back({z,x,val});
if(x>y)res[z].push_back({x,y,val});
else res[z].push_back({y,x,val});
}
void dfs2(int x){
for(auto [v,val]:e[x]){
if(!ord(x,v))continue;
tag[v]={x,val};
}
for(auto [v,val]:e[x]){
if(!ord(x,v))continue;
for(auto [v2,val2]:e[v]){
if(!ord(v,v2))continue;
if(tag[v2].first==x){
report(x,v,v2,tag[v2].second+val+val2);
}
}
}
}
int solve(int x){
if(res[x].size()<=1)return -1;
int max_z,max_y,max_v=-1;
vector<pair<int,int>>s;
for(auto [y,z,val]:res[x]){
if(val>max_v){
max_v=val;
}
}
int ans=-1;
for(auto [y,z,val]:res[x]){
if(val==max_v){
s.push_back({y,z});
}
}
for(auto [max_y,max_z]:s){
pair<int,int> fix_y={-1,-1},fix_z={-1,-1};
int libre=-1;
for(auto [y,z,val]:res[x]){
if(max_y==y){
if(max_z!=z){
fix_y=max(fix_y,{val,z});
}
}
else if(max_z==z){
if(max_y!=y){
fix_z=max(fix_z,{val,y});
}
}
else if(max_z==y){
if(max_y!=z){
fix_z=max(fix_z,{val,z});
}
}
else if(max_y==z){
if(max_z!=y){
fix_y=max(fix_y,{val,y});
}
}
else {
// cout<<"safe "<<y<<" "<<z<<endl;
libre=max(libre,val);}
}
// cout<<x<<" "<<max_y<<" "<<max_z<<' '<<fix_y.second<<" "<<fix_z.second<<" "<<libre<<endl;
if(libre==-1&&(fix_y.first==-1||fix_z.first==-1))ans=max(ans,-1);
else ans=max(ans,max(max_v+libre,fix_y.first+fix_z.first));
if(ans==max_v*2)return ans;
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
e[x].push_back({y,z});
e[y].push_back({x,z});
}
for(int i=1;i<=n;i++){
dfs2(i);
}
int ans=-1;
for(int i=1;i<=n;i++){
ans=max(ans,solve(i));
}
printf("%d\n",ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
6 9 1 2 3 2 3 1 3 4 2 4 5 1 3 5 7 2 5 2 1 5 3 4 6 2 5 6 1