【CodeForces】990F Flow Control
題目鏈接
Luogu & CodeForces
題目描述
You have to handle a very complex water distribution system. The system consists of nn junctions and mm pipes, ii -th pipe connects junctions xixi and yiyi .
The only thing you can do is adjusting the pipes. You have to choose mm
integer numbers
In order for the system to work properly, there are some constraints: for every i∈[1,n]i∈[1,n]
, ii
-th junction has a number sisi
associated with it meaning that the difference between incoming and outcoming flow for
Can you choose the integers f1f1 , f2f2 , ..., fmfm in such a way that all requirements on incoming and outcoming flows are satisfied?
題目翻譯
現在有一幅圖,節點由水管連接起來,現在每個水管裏都有一些水流,請問能否使每個節點的輸入或輸出的水流大小等於指定的大小。
解題思路
首先,肯定得判斷能不能構造。
那麽我們怎麽判斷呢?
假設一條水管連通x和y兩個點,那麽假設這條水管內的水流量為a,從x到y,那麽x的水流量應該-a,而y的水流量應該+a,顯然,兩個節點的水量的和應該使0,那麽我們直接判斷每個節點的水量之和是不是0就好了。
接下來就是構造了,每兩個節點之間都可能有很多條路徑能夠到達,我們只取一條,也就是構造一棵樹,然後從最底層向上推,推出每根水管的水量。
代碼
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 using namespace std; 8 const int maxn=400050; 9 int n,m,cnt=0; 10 struct nod{ 11 int t,val; 12 }; 13 vector<nod>v[maxn]; 14 int jun[maxn],ans[maxn]; 15 bool vis[maxn]; 16 map<int,int>ma[400050]; 17 inline void read(register int &x){ 18 int f=1; 19 x=0; register char ch=getchar(); 20 while(ch<‘0‘||ch>‘9‘){ 21 if(ch==‘-‘)f=-1; 22 ch=getchar(); 23 } 24 while(ch>=‘0‘&&ch<=‘9‘)x=x*10+ch-‘0‘,ch=getchar(); 25 x=x*f; 26 } 27 inline void dfs(int now,int fa,int num){ 28 vis[now]=1; 29 int tot=0,flag=0; 30 for(register int i=0;i<v[now].size();i++){ 31 if(v[now][i].t==fa||vis[v[now][i].t])continue; 32 flag=1; 33 dfs(v[now][i].t,now,i); 34 tot+=v[now][i].val; 35 } 36 if(!flag){ 37 ans[ma[fa][now]]=jun[now]; 38 v[fa][num].val=jun[now]; 39 return; 40 } 41 ans[ma[fa][now]]=jun[now]+tot; 42 v[fa][num].val=jun[now]+tot; 43 } 44 int main(){ 45 read(n); 46 long long tot=0; 47 for(register int i=1;i<=n;i++){ 48 read(jun[i]); 49 tot+=jun[i]; 50 } 51 read(m); 52 register int f,t; 53 for(register int i=1;i<=m;i++){ 54 read(f),read(t); 55 ma[f][t]=i; 56 ma[t][f]=i; 57 nod temp; 58 temp.t=t,temp.val=0; 59 v[f].push_back(temp); 60 temp.t=f; 61 v[t].push_back(temp); 62 } 63 if(tot!=0){ 64 cout<<"Impossible"<<endl; 65 return 0; 66 } 67 nod temp; 68 temp.t=1; 69 v[0].push_back(temp); 70 dfs(1,0,0); 71 cout<<"Possible"<<endl; 72 for(register int i=1;i<=n;i++){ 73 printf("%d\n",ans[i]); 74 } 75 }
【CodeForces】990F Flow Control