CF990F Flow Control 题解
解题思路
一个很显然的结论:
- 当且仅当所有点的流量之和为
时,存在解。
在得出这个结论后,我们可以先计算出 Impossible
即可。
接下来讨论为
时间复杂度
AC 代码
#include<iostream>
#include<math.h>
#include<time.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include <map>
#define ll long long
inline void FileOpen(const char *infile,const char *outfile){
char *fileread=new char[1];
char *filewrite=new char[1];
fileread[0]=114,filewrite[0]=119;
freopen(infile,fileread,stdin);
freopen(outfile,filewrite,stdout);
}
inline void FileClose(){
char *fileread=new char[1];
char *filewrite=new char[1];
char *CONwrite=new char[3];
fileread[0]=114,filewrite[0]=119;
CONwrite[0]=67,CONwrite[1]=79,CONwrite[2]=78;
fclose(stdin);
fclose(stdout);
std::cin.clear();
std::cout.clear();
freopen(CONwrite,fileread,stdin);
freopen(CONwrite,filewrite,stdout);
}
#define N 200005
#define pii std::pair<int,int>
int n,m,s[N],f[N];
int fl[N],vis[N];
std::vector<int> edge[N];
std::map<pii,int> pos;
inline void dfs(int u,int fa){
fl[u]=s[u];vis[u]=1;
for(auto v:edge[u]){
if(v==fa) continue;
if(vis[v]) continue;
dfs(v,u);fl[u]+=fl[v];
}if(fl[u]==0) return;
int np=pos[{u,fa}];
if(np<0)
f[-np]=fl[u];
else f[np]=-fl[u];
}
inline void work(){
scanf("%d",&n);int sum=0;
for(register int i=1;i<=n;++i)
scanf("%d",&s[i]),sum+=s[i];
if(sum!=0){puts("Impossible");return;}
scanf("%d",&m);int u,v;
for(register int i=1;i<=m;++i){
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
pos[{u,v}]=i;
pos[{v,u}]=-i;
}dfs(1,0);puts("Possible");
for(register int i=1;i<=m;++i)
printf("%d\n",f[i]);
}signed main(){
srand(114514);
srand(rand());
srand(time(0));
work();system("pause");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下