답안 #885547

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885547 2023-12-10T04:01:18 Z 8pete8 Pipes (BOI13_pipes) C++17
72.7778 / 100
336 ms 31080 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
const int mod=1e9+7,mxn=1e5+5,lg=30,inf=1e18,minf=-1e18;
int ans[mxn+10],val[mxn+10],up[mxn+10],curval[mxn+10];
vector<pii>adj[mxn+10];
bitset<mxn+10>vis;
bool yes=false;
void solve(int cur,int p){
    if(vis[cur])return void(yes=true);
    vis[cur]=true;
    bool leaf=true;
    for(auto i:adj[cur])if(i.f!=p)leaf=false;
    if(leaf){
        ans[up[cur]]+=val[cur];
        curval[p]+=val[cur];
        return;
    }
    for(auto i:adj[cur]){
        if(i.f==p)continue;
        up[i.f]=i.s;
        solve(i.f,cur);
    }
    if(cur==1)return;
    ans[up[cur]]+=(val[cur]-curval[cur]);
    curval[p]+=(val[cur]-curval[cur]);
}
vector<ppii>path;
int deg[mxn+10],sum,cnt=0;
int start=1;
void solve2(int cur,int p){
    if(vis[cur])return;
    cnt++;
    sum+=(val[cur]-curval[cur])*((cnt%2)?1:-1);
    vis[cur]=true;
    for(auto i:adj[cur]){
        if(i.f==start&&i.f!=p)path.pb({i.s,{i.f,cur}});
        if(i.f==p||vis[i.f])continue;
        up[i.f]=i.s;
        path.pb({i.s,{i.f,cur}});
        solve2(i.f,cur);
    }
}
int32_t main(){
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>val[i];
    for(int i=0;i<m;i++){
        int a,b;cin>>a>>b;
        adj[a].pb({b,i});
        adj[b].pb({a,i});
    }
    if(m>n){
        cout<<0;
        return 0;
    }
    if(m==n-1){
        solve(1,-1);
        for(int i=0;i<m;i++)cout<<2*ans[i]<<'\n';
        return 0;
    }
    stack<int>st;
    for(int i=1;i<=n;i++){
        if(adj[i].size()==1)st.push(i);
        deg[i]=adj[i].size();
    }
    cnt=0;
    while(!st.empty()){
        vis[st.top()]=true;
        for(auto i:adj[st.top()]){
            if(vis[i.f])continue;
            deg[i.f]--;
            ans[i.s]+=val[st.top()]-curval[st.top()];
            curval[i.f]+=val[st.top()]-curval[st.top()];
            curval[st.top()]+=val[st.top()]-curval[st.top()];
            if(deg[i.f]==1)st.push(i.f);
        }
        st.pop();
        cnt++;
    }
    for(int i=1;i<=n;i++)if(!vis[i])start=i;
    for(int i=1;i<=n;i++)if(!vis[i]&&curval[i]!=0)start=i;
    if((n-cnt)%2==0){
        cout<<0;
        return 0;
    }
    cnt=0;
    solve2(start,-1);
    sum/=2;
    reverse(all(path));
    for(int i=0;i<path.size();i++){
        if(i==0){
            ans[path[i].f]+=sum;
            curval[path[i].s.f]+=sum;
            curval[path[i].s.s]+=sum;
        }
        else{
            ans[path[i].f]+=val[path[i].s.f]-curval[path[i].s.f];
            curval[path[i].s.f]+=val[path[i].s.f]-curval[path[i].s.f];
            curval[path[i].s.s]+=val[path[i].s.f]-curval[path[i].s.f];
        }
    }
    for(int i=0;i<m;i++)cout<<2*ans[i]<<'\n';
}

Compilation message

pipes.cpp: In function 'int32_t main()':
pipes.cpp:121:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for(int i=0;i<path.size();i++){
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 82 ms 11888 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 59 ms 10320 KB Output is correct
14 Correct 72 ms 11608 KB Output is correct
15 Correct 86 ms 11960 KB Output is correct
16 Correct 65 ms 10792 KB Output is correct
17 Correct 76 ms 11876 KB Output is correct
18 Correct 77 ms 11964 KB Output is correct
19 Correct 81 ms 15224 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 76 ms 11872 KB Output is correct
23 Correct 60 ms 10324 KB Output is correct
24 Correct 77 ms 11908 KB Output is correct
25 Correct 66 ms 10576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4444 KB Output isn't correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Correct 66 ms 11088 KB Output is correct
4 Correct 65 ms 10320 KB Output is correct
5 Correct 63 ms 10068 KB Output is correct
6 Correct 283 ms 31080 KB Output is correct
7 Incorrect 1 ms 4440 KB Output isn't correct
8 Incorrect 1 ms 4440 KB Output isn't correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Incorrect 1 ms 4444 KB Output isn't correct
15 Incorrect 2 ms 4700 KB Output isn't correct
16 Incorrect 1 ms 4444 KB Output isn't correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4700 KB Output is correct
22 Incorrect 1 ms 4444 KB Output isn't correct
23 Incorrect 70 ms 20196 KB Output isn't correct
24 Incorrect 88 ms 20548 KB Output isn't correct
25 Correct 65 ms 11088 KB Output is correct
26 Correct 68 ms 10532 KB Output is correct
27 Correct 66 ms 10424 KB Output is correct
28 Correct 68 ms 10576 KB Output is correct
29 Correct 214 ms 25936 KB Output is correct
30 Incorrect 65 ms 11328 KB Output isn't correct
31 Incorrect 90 ms 24536 KB Output isn't correct
32 Incorrect 81 ms 15756 KB Output isn't correct
33 Correct 88 ms 11420 KB Output is correct
34 Correct 66 ms 10320 KB Output is correct
35 Correct 67 ms 10620 KB Output is correct
36 Correct 69 ms 10324 KB Output is correct
37 Correct 336 ms 31056 KB Output is correct
38 Incorrect 98 ms 20216 KB Output isn't correct
39 Incorrect 68 ms 11580 KB Output isn't correct
40 Incorrect 97 ms 20488 KB Output isn't correct
41 Incorrect 94 ms 24248 KB Output isn't correct
42 Correct 74 ms 10428 KB Output is correct
43 Correct 66 ms 10320 KB Output is correct
44 Correct 64 ms 10052 KB Output is correct
45 Correct 253 ms 28588 KB Output is correct
46 Incorrect 68 ms 11168 KB Output isn't correct
47 Incorrect 76 ms 11592 KB Output isn't correct
48 Incorrect 95 ms 23792 KB Output isn't correct
49 Correct 71 ms 11344 KB Output is correct
50 Correct 68 ms 10320 KB Output is correct
51 Correct 65 ms 10352 KB Output is correct
52 Correct 65 ms 10320 KB Output is correct
53 Correct 228 ms 27848 KB Output is correct
54 Incorrect 66 ms 11348 KB Output isn't correct