이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <complex>
#include <stack>
#include <set>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double>
const int INF = 1e9;
using namespace std;
const int MAXN = 100*1000 +5;
int nodeVal[MAXN];
int edgesVal[MAXN];
bool marked[MAXN];
vector<ii> g[MAXN];
stack<int> st;
bool vis[MAXN];
void markCycle(int node,int p){
st.push(node);
if(vis[node]){
while(!st.empty()){
int e = st.top();st.pop();
marked[e] = 1;
}
}else{
vis[node] = 1;
for(auto e:g[node]){
if(e.ff != p){
markCycle(e.ff,node);
}
}
}
if(!st.empty())st.pop();
}
void dfs(int node,int p,int pe){
for(auto e:g[node]){
if(e.ff != p and !marked[e.ff]){
dfs(e.ff,node,e.ss);
nodeVal[node] -= nodeVal[e.ff];
}
}
if(pe > -1){
edgesVal[pe] = nodeVal[node];
}
}
int main(){
int n,m;
cin >> n >> m;
if(m > n){
cout << "0" << endl;
}else if(m < n){
FOR(i,n)cin >> nodeVal[i];
FOR(i,m){
int a,b;
cin >> a >> b;
a--;b--;
g[a].pb({b,i});
g[b].pb({a,i});
}
dfs(0,-1,-1);
FOR(i,m){
cout << 2*edgesVal[i] << endl;
}
}else{
markCycle(0,-1);
int cnt = 0;
FOR(i,n){
if(marked[i]){
dfs(i,-1,-1);
cnt++;
}
}
if(cnt%2 == 0){
cout << 0 << endl;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |