QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#301534 | #7991. 最小环 | 111445# | RE | 124ms | 96060kb | C++23 | 3.0kb | 2024-01-10 02:11:27 | 2024-01-10 02:11:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 600000
int i,j,k,n,m,t,fa[N+50],in[N+50],dfn[N+50],it;
ll res=1e18;
vector<pair<int,int> > v2[N+50];
vector<tuple<int,int,int> > bian,v[N+50];
set<int> s0,s1;
int find(int x){return (fa[x]==x)?x:fa[x]=find(fa[x]);}
ll f[N+50];
ll dep[N+50],g[N+50],dep2[N+50];
struct SB{
int i,j,k,n;
int f[N+50][21],l2[N+50];
int _get(int l,int r){
return (dfn[l]<dfn[r]?l:r);
}
void init(int nn){
n=nn;
for(i=1;i<=20;i++)for(j=1;j+(1<<i)-1<=n;j++)f[j][i]=_get(f[j][i-1],f[j+(1<<(i-1))][i-1]);
}
int lca(int l,int r){
if(l==r)return l;
l=dfn[l]; r=dfn[r]; if(l>r)swap(l,r);
l++; int k=l2[r-l+1];
return _get(f[l][k],f[r-(1<<k)+1][k]);
}
}sb;
void dfs(int x,int fa){
dfn[x]=++it; sb.f[it][0]=fa;
//cout<<"nmsl "<<x<<' '<<fa<<' '<<it<<endl;
for(auto [i,j,k]:v[x])if(i!=fa){
g[i]=g[x]+k;
dep[i]=dep[x]+j;
dep2[i]=dep2[x]+1;
dfs(i,x);
}
}
bool cmp(int x,int y){return dfn[x]<dfn[y];}
void fuck0(){
vector<int> tmp={1},tmp2;
for(auto i:s0)tmp.push_back(i);
sort(tmp.begin(),tmp.end(),cmp);
int sz=tmp.size(),i;
for(int i=0;i<sz;i++){
//cout<<"CNM "<<tmp[i]<<' '<<tmp[i+1]<<' '<<sb.lca(tmp[i],tmp[i+1])<<endl;
tmp2.push_back(tmp[i]);
if(i!=sz-1)tmp2.push_back(sb.lca(tmp[i],tmp[i+1]));
}
sort(tmp2.begin(),tmp2.end(),cmp);
tmp2.erase(unique(tmp2.begin(),tmp2.end()),tmp2.end());
//cout<<"NMSL"<<endl; for(auto i:tmp2){cout<<i<<' ';}cout<<endl;
for(auto i:tmp2)s1.insert(i);
sz=tmp2.size();
for(i=0;i<sz-1;i++){
int x=tmp2[i],y=tmp2[i+1];
x=sb.lca(x,y);
//cout<<"NMSL "<<x<<' '<<y<<' '<<g[x]<<' '<<g[y]<<' '<<dep2[x]<<' '<<dep2[y]<<endl;
if(g[y]-g[x]==0){
v2[y].push_back({x,dep[y]-dep[x]});
}
if(g[y]-g[x]==dep2[y]-dep2[x]){
//cout<<"CNM "<<x<<' '<<y<<' '<<dep[y]-dep[x]<<endl;
v2[x].push_back({y,dep[y]-dep[x]});
}
}
}
ll fuck(ll st,ll ed){
for(auto i:s1){
//cout<<"CNMNMSL "<<i<<endl;
f[i]=1e18;
}
priority_queue<pair<ll,ll> > q;
q.push({0,st});
while(!q.empty()){
auto [w,id]=q.top(); q.pop(); w=-w;
//cout<<"NMSL "<<st<<' '<<id<<' '<<w<<' '<<f[id]<<endl;
if(f[id]<=w)continue;
f[id]=w;
for(auto [i,j]:v2[id])if(f[i]>w+j){
q.push({-(w+j),i});
}
}
return f[ed];
}
int main(){
for(i=2;i<=N;i++)sb.l2[i]=sb.l2[i/2]+1;
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
for(i=1;i<=n;i++)fa[i]=i;
for(i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
if(find(x)==find(y)){
v2[x].push_back({y,z});
bian.push_back({x,y,z});
s0.insert(x); s0.insert(y);
//cout<<"nmsl "<<x<<' '<<y<<' '<<z<<endl;
continue;
}
//cout<<"nmsl "<<x<<' '<<y<<' '<<z<<endl;
fa[find(x)]=find(y);
v[x].push_back({y,z,1});
v[y].push_back({x,z,0});
in[y]++;
}
if(n>=100000&&n<=250000)return 1;
dfs(1,0);
sb.init(n);
fuck0();
int NMSL=0;
for(i=1;i<=n;i++){
NMSL+=v2[i].size();
}
for(auto [l,r,w]:bian){
res=min(res,fuck(r,l)+w);
}
if(res>1e17)res=-1;
cout<<res;
}
详细
Test #1:
score: 100
Accepted
time: 7ms
memory: 42752kb
input:
4 6 1 2 1 4 3 3 4 1 9 2 4 1 3 1 2 3 2 6
output:
7
result:
ok single line: '7'
Test #2:
score: 0
Accepted
time: 7ms
memory: 36716kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 3ms
memory: 39152kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 124ms
memory: 96060kb
input:
258420 258419 33061 33062 767169384 212916 212917 1741339 229881 229882 896760805 173467 173468 273055172 233189 233190 800433307 10157 10158 126766550 174605 174606 552176083 224030 224031 886617880 229102 229103 783848581 67588 67589 510826095 233648 233649 879695751 214453 214454 867104578 153140...
output:
-1
result:
ok single line: '-1'
Test #5:
score: -100
Runtime Error
input:
248016 248896 82688 82755 592665252 202847 203260 294408121 26821 28237 269132335 150967 152178 3125829 246069 247390 29492546 7470 7673 55843492 33975 35414 442802995 28451 28948 193736988 34484 34637 84441058 60168 60309 501991354 79579 79844 26854803 239672 239706 111702667 73548 73658 149840530 ...