QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#863502 | #4809. Maximum Range | yhddd | WA | 15ms | 17480kb | C++20 | 3.3kb | 2025-01-19 18:12:51 | 2025-01-19 18:12:52 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=100010;
const int inf=1e18;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
return x*f;
}
bool Mbe;
int n,m;
pair<pii,int> g[maxn];
int head[maxn],tot;
struct nd{
int nxt,to,w;
}e[maxn<<1];
void add(int u,int v,int w){e[++tot]={head[u],v,w};head[u]=tot;}
int dfn[maxn],lw[maxn],idx;
int st[maxn],tp;
int scc[maxn],scct;
void tar(int u){
dfn[u]=lw[u]=++idx;st[++tp]=u;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]){
tar(v);
lw[u]=min(lw[u],lw[v]);
}
else if(!scc[v])lw[u]=min(lw[u],dfn[v]);
}
if(dfn[u]==lw[u]){
scc[st[tp]]=++scct;
while(st[tp--]!=u)scc[st[tp]]=scct;
}
}
int mn[maxn],mx[maxn];
void addflow(int u,int v,int w){
add(u,v,w),add(v,u,0);
}
int s,t,flow;
int dis[maxn],rad[maxn];
queue<int> q;
bool bfs(){
for(int i=1;i<=t;i++)dis[i]=0,rad[i]=head[i];
dis[s]=1;q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(!dis[v]&&e[i].w)dis[v]=dis[u]+1,q.push(v);
}
}
return dis[t];
}
int dfs(int u,int val){
if(u==t)return val;
int res=0;
for(int i=rad[u];i;i=e[i].nxt){
int v=e[i].to;rad[u]=i;
if(dis[v]==dis[u]+1&&e[i].w){
int out=dfs(v,min(e[i].w,val));
e[i].w-=out,e[i^1].w+=out;
val-=out,res+=out;
if(!val)break;
}
}
return res;
}
vector<int> res;
bool vis[maxn];
void get(int u){
if(u<=n){
if(vis[u]){
while(res.back()!=u)res.pop_back();
}
else res.pb(u),vis[u]=1;
}
if(u==t)return ;
for(int i=rad[u];i;i=e[i].nxt){
int v=e[i].to;
if(!e[i].w){
rad[u]=e[i].nxt;
return get(v);
}
}
}
void work(){
n=read();m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
g[i]={{u,v},w};
}
tar(1);
for(int i=1;i<=scct;i++)mn[i]=inf,mx[i]=-inf;
for(int i=1;i<=n;i++)head[i]=0;tot=1;
for(int i=1;i<=m;i++){
auto[p,w]=g[i];auto[u,v]=p;
if(scc[u]!=scc[v])continue;
mn[scc[u]]=min(mn[scc[u]],w),mx[scc[u]]=max(mx[scc[u]],w);
}
pii ans={0,0};for(int i=1;i<=scct;i++)ans=max(ans,{mx[i]-mn[i],i});
s=n+1,t=n+2;
for(int i=1;i<=m;i++){
auto[p,w]=g[i];auto[u,v]=p;
if(scc[u]!=scc[v]||scc[u]!=ans.se)continue;
addflow(u,v,1),addflow(v,u,1);
if(w==mn[scc[u]]){
addflow(s,u,1),addflow(s,v,1);
mn[scc[u]]=inf;
}
if(w==mx[scc[u]]){
addflow(u,t,1),addflow(v,t,1);
mx[scc[u]]=-inf;
}
}
for(int i=1;i<=2;i++)if(bfs())flow+=dfs(s,1);
// for(int i=2;i<=tot;i+=2)cout<<e[i^1].to<<" "<<e[i].to<<" "<<e[i].w<<"\n";
// cout<<flow<<"\n";
for(int i=1;i<=t;i++)rad[i]=head[i];
get(s);reverse(res.begin(),res.end());
for(int i=1;i<=n;i++)vis[i]=0;
get(s);
printf("%lld\n%lld\n",ans.fi,res.size());
for(int i:res)printf("%lld ",i);
}
// \
444
bool Med;
int T;
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
T=1;
while(T--)work();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 8040kb
input:
5 7 1 2 1 1 3 -2 2 3 1 3 4 3 4 5 1 1 5 -1 2 5 2
output:
5 4 3 1 5 4
result:
ok ok
Test #2:
score: -100
Wrong Answer
time: 15ms
memory: 17480kb
input:
99997 100000 12238 99016 352755196 99016 25485 -412473602 25485 2440 991507552 2440 31171 -181894654 36970 2440 -800167579 2440 41865 -148191946 96629 31171 847888506 36970 95740 395546542 27992 2440 647886610 99016 29557 369124914 80795 27992 -673871966 36970 3509 573208857 57672 29557 874406776 41...
output:
1999987695 0
result:
wrong answer Integer 0 violates the range [1, 100000]