Submission #555934

# Submission time Handle Problem Language Result Execution time Memory
555934 2022-05-01T20:35:49 Z new_acc Pipes (BOI13_pipes) C++14
100 / 100
154 ms 38092 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[N],ans[N],val[N];
vector<pair<int,int> >graf[N];
vi __curr;
bool vis[N];
void wa(){
    cout<<0<<"\n";
    exit(0);
}
ll dfs(int v){
    if(vis[v]) return 0;
    vis[v]=1;
    ll sum=0;
    for(auto u:graf[v]){
        ll curr=dfs(u.fi);
        sum+=curr,ans[u.se]=(curr<<1);
    }
    return (ll)t[v]-sum;
}
void dfs2(int v,vi *c,int o,int num){
    if(c->size()) return;
    val[v]=num;
    if(vis[v]){
        c->push_back(v);
        while(__curr[__curr.size()-1]!=v) c->push_back(__curr[__curr.size()-1]),__curr.pop_back();
        return;
    }
    vis[v]=1;
    __curr.push_back(v);
    for(auto u:graf[v]) if(o!=u.fi) dfs2(u.fi,c,v,u.se);
    __curr.pop_back();
}
void solve(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>t[i];
    for(int a,b,i=1;i<=m;i++){
        cin>>a>>b;
        graf[a].push_back({b,i}),graf[b].push_back({a,i});
    }
    if(m>n) wa();
    if(m==n-1){
        if(dfs(1)) wa();
        for(int i=1;i<=m;i++) cout<<ans[i]<<"\n";
        return;
    }
    vi c;
    dfs2(1,&c,1,0);
    if(!(c.size()&1)) wa();
    reverse(c.begin(),c.end());
    for(int i=1;i<=n;i++) vis[i]=0;
    for(int i=0;i<c.size();i++){
        int pop=(i==0?c[c.size()-1]:c[i-1]),nxt=(i==c.size()-1?c[0]:c[i+1]);
        vis[pop]=1,vis[nxt]=1;
        t[c[i]]=dfs(c[i]);
        vis[pop]=0,vis[nxt]=0;
    }
    ll sum1=0,sum2=0;
    for(int i=1;i<c.size();i+=2) sum1+=(ll)t[c[i]];
    sum1<<=1LL;
    for(int i=0;i<c.size();i++) sum2+=(ll)t[c[i]];
    ans[val[c[0]]]=sum2-sum1;
    if(ans[val[c[0]]]&1LL) wa();
    for(int i=1;i<c.size();i++) ans[val[c[i]]]=(t[c[i-1]]-(ans[val[c[i-1]]]>>1))<<1LL;
    if(t[c[c.size()-1]]!=((ans[val[c[c.size()-1]]]+ans[val[c[0]]])>>1)) wa();
    for(int i=1;i<=m;i++) cout<<ans[i]<<"\n";
}
int main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    int tt=1;
    while(tt--) solve();
}

Compilation message

pipes.cpp: In function 'void solve()':
pipes.cpp:70:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i=0;i<c.size();i++){
      |                 ~^~~~~~~~~
pipes.cpp:71:51: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         int pop=(i==0?c[c.size()-1]:c[i-1]),nxt=(i==c.size()-1?c[0]:c[i+1]);
      |                                                  ~^~~~~~~~~~~~
pipes.cpp:77:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i=1;i<c.size();i+=2) sum1+=(ll)t[c[i]];
      |                 ~^~~~~~~~~
pipes.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for(int i=0;i<c.size();i++) sum2+=(ll)t[c[i]];
      |                 ~^~~~~~~~~
pipes.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for(int i=1;i<c.size();i++) ans[val[c[i]]]=(t[c[i-1]]-(ans[val[c[i-1]]]>>1))<<1LL;
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23764 KB Output is correct
2 Correct 13 ms 23728 KB Output is correct
3 Correct 12 ms 23840 KB Output is correct
4 Correct 72 ms 28788 KB Output is correct
5 Correct 14 ms 23792 KB Output is correct
6 Correct 11 ms 23832 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23864 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 56 ms 27812 KB Output is correct
14 Correct 65 ms 28460 KB Output is correct
15 Correct 64 ms 28972 KB Output is correct
16 Correct 53 ms 28028 KB Output is correct
17 Correct 61 ms 28828 KB Output is correct
18 Correct 68 ms 28828 KB Output is correct
19 Correct 73 ms 31292 KB Output is correct
20 Correct 13 ms 23832 KB Output is correct
21 Correct 13 ms 23776 KB Output is correct
22 Correct 60 ms 28792 KB Output is correct
23 Correct 51 ms 27788 KB Output is correct
24 Correct 60 ms 28876 KB Output is correct
25 Correct 55 ms 27916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23720 KB Output is correct
2 Correct 12 ms 23940 KB Output is correct
3 Correct 62 ms 31288 KB Output is correct
4 Correct 49 ms 27388 KB Output is correct
5 Correct 44 ms 27588 KB Output is correct
6 Correct 152 ms 38044 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 14 ms 23764 KB Output is correct
12 Correct 12 ms 23724 KB Output is correct
13 Correct 12 ms 23744 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 13 ms 23892 KB Output is correct
16 Correct 13 ms 23828 KB Output is correct
17 Correct 12 ms 23892 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 13 ms 23892 KB Output is correct
23 Correct 68 ms 31688 KB Output is correct
24 Correct 71 ms 32716 KB Output is correct
25 Correct 57 ms 31188 KB Output is correct
26 Correct 52 ms 27468 KB Output is correct
27 Correct 59 ms 27284 KB Output is correct
28 Correct 54 ms 27748 KB Output is correct
29 Correct 128 ms 35408 KB Output is correct
30 Correct 74 ms 34808 KB Output is correct
31 Correct 77 ms 34980 KB Output is correct
32 Correct 67 ms 30372 KB Output is correct
33 Correct 53 ms 32040 KB Output is correct
34 Correct 45 ms 27408 KB Output is correct
35 Correct 49 ms 27432 KB Output is correct
36 Correct 47 ms 27724 KB Output is correct
37 Correct 154 ms 38092 KB Output is correct
38 Correct 77 ms 32204 KB Output is correct
39 Correct 63 ms 29900 KB Output is correct
40 Correct 77 ms 32512 KB Output is correct
41 Correct 56 ms 34412 KB Output is correct
42 Correct 46 ms 27240 KB Output is correct
43 Correct 47 ms 27340 KB Output is correct
44 Correct 45 ms 27588 KB Output is correct
45 Correct 117 ms 36296 KB Output is correct
46 Correct 78 ms 35392 KB Output is correct
47 Correct 77 ms 32488 KB Output is correct
48 Correct 73 ms 34756 KB Output is correct
49 Correct 55 ms 29132 KB Output is correct
50 Correct 46 ms 27492 KB Output is correct
51 Correct 46 ms 27640 KB Output is correct
52 Correct 45 ms 27388 KB Output is correct
53 Correct 141 ms 36060 KB Output is correct
54 Correct 90 ms 33592 KB Output is correct