Submission #90619

#TimeUsernameProblemLanguageResultExecution timeMemory
90619psmaoPipes (BOI13_pipes)C++14
100 / 100
108 ms9588 KiB
#include <bits/stdc++.h>
using namespace std;

#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}

const int maxn = 100050;

int n, m, deg[maxn], s[maxn];
ll v[maxn];
vector<pii> adj[maxn];
queue<int> Q;
VI pathx, nodex;
bool vis[maxn];

int main()
{
	#ifdef MPS
		fp("1.in","r",stdin);
		fp("1.out","w",stdout);
	#endif
	sf("%d%d",&n,&m);
	if(m != n-1 && m != n) return 0 * pf("0\n");
	fo(i,1,n) sf("%d",&s[i]);
	fo(i,1,m) 
	{
		int u, v; sf("%d%d",&u,&v); 
		adj[u].pb(mp(v,i)); 
		adj[v].pb(mp(u,i)); 
		deg[u] ++; deg[v] ++;
	}
	fo(i,1,n) if(deg[i] == 1) Q.push(i);
	while(!Q.empty())
	{
		int h = Q.front(); Q.pop();
		vis[h] = true;
		for(auto p : adj[h]) if(!vis[p.fi])
		{
			v[p.se] = s[h];
			s[p.fi] -= s[h];
			if((--deg[p.fi]) == 1) Q.push(p.fi);
		}
	}
	fo(i,1,n) if(deg[i] == 2)
	{
		int x = i; ll cur = 0ll;
		while(1)
		{
			bool contin = false; deg[x] --;
			cur = s[x] - cur; nodex.pb(x);
			for(auto p : adj[x]) if(deg[p.fi] == 2) {x = p.fi; pathx.pb(p.se); contin = true; break;}
			if(!contin) // we have iterated the cycle already
			{
				if(!(SZ(nodex)&1)) return 0 * pf("0\n");
				for(auto p : adj[x]) if(p.fi == i) {pathx.pb(p.se); break;}
				if(cur&1) return 0 * pf("0\n");
				cur >>= 1;
				v[pathx.back()] = cur;
				for(int p = 0; p < SZ(pathx)-1; ++ p) {cur = s[nodex[p]] - cur; v[pathx[p]] = cur;} 
				break;
			}
		}
		break;
	}
	fo(i,1,m) pf("%lld\n", v[i]*2);
	return 0;
}

Compilation message (stderr)

pipes.cpp: In function 'int main()':
pipes.cpp:48:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d%d",&n,&m);
    ^
pipes.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fo(i,1,n) sf("%d",&s[i]);
              ^
pipes.cpp:53:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; sf("%d%d",&u,&v); 
               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...