Submission #221959

# Submission time Handle Problem Language Result Execution time Memory
221959 2020-04-11T15:33:57 Z XmtosX Pipes (BOI13_pipes) C++17
100 / 100
251 ms 17272 KB
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,o;
long long a[N],sz[N],ans[N];
vector <pair<int,int> > v[N];
queue<int> q;
bool vis[N];
long long sum (int x)
{
    vis[x]=true;
    long long here=0;
    bool yes=false;
    for (int i=0;i<v[x].size();i++)
    {
        if (!vis[v[x][i].first])
        {
            yes=true;
            here=sum(v[x][i].first);
            break;
        }
    }
    vis[x]=false;
    return here+a[x];
}
long long dfs1 (int x,long long last)
{
    vis[x]=true;
    bool yes=false;
    long long here=0;
    for (int i=0;i<v[x].size();i++)
    {
        if (!vis[v[x][i].first])
        {
            here=dfs1(v[x][i].first,a[v[x][i].first]-last);
            yes=true;
            break;
        }
    }
    vis[x]=false;
    return here+last;
}
void dfs2 (int x)
{
    vis[x]=true;
    bool yes=false;
    for (int i=0;i<v[x].size();i++)
    {
        if (!vis[v[x][i].first])
        {
            a[v[x][i].first]-=a[x];
            ans[v[x][i].second]=a[x];
            dfs2(v[x][i].first);
            yes=true;
            break;
        }
    }
    if (!yes)
    {
        for (int i=0;i<v[x].size();i++)
        {
            if (v[x][i].first==o)
            {
                ans[v[x][i].second]=a[x];
                break;
            }
        }
    }
    vis[x]=false;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for (int i=0;i<m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        v[a].push_back({b,i});
        v[b].push_back({a,i});
    }
    if (m>n)
    {
        cout <<0;
        return 0;
    }
    for (int i=1;i<=n;i++)
    {
        sz[i]=v[i].size();
        if (v[i].size()==1)
        {
            q.push(i);
        }
    }
    int cnt=n;
    while (!q.empty())
    {
        int x= q.front();
        q.pop();
        cnt--;
        vis[x]=true;
        for (int i=0;i<v[x].size();i++)
        {
            int nxt=v[x][i].first;
            int idx=v[x][i].second;
            if (!vis[nxt])
            {
                ans[idx]=a[x];
                a[nxt]-=a[x];
                sz[nxt]--;
                if (sz[nxt]==1)
                    q.push(nxt);
            }
        }
    }
    if (cnt%2==0&&cnt)
    {
        cout <<0;
        return 0;
    }
    for (int i=1;i<=n;i++)
    {
        if (!vis[i])
        {
            o=i;
            long long x=sum(i);
            x/=2;
            long long y=dfs1(i,0);
            a[i]= x-y;
            dfs2(i);
            break;
        }
    }
    for (int i=0;i<m;i++)
        printf("%lld\n",ans[i]*2ll);
    return 0;
}
/*
4 3
-1
1
-3
1
1 2
1 3
1 4

4 5
1
2
1
2
1 2
2 3
3 4
4 1
1 3

3 3
1
15
-4
1 2
2 3
3 1
*/

Compilation message

pipes.cpp: In function 'long long int sum(int)':
pipes.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                  ~^~~~~~~~~~~~
pipes.cpp:13:10: warning: variable 'yes' set but not used [-Wunused-but-set-variable]
     bool yes=false;
          ^~~
pipes.cpp: In function 'long long int dfs1(int, long long int)':
pipes.cpp:31:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                  ~^~~~~~~~~~~~
pipes.cpp:29:10: warning: variable 'yes' set but not used [-Wunused-but-set-variable]
     bool yes=false;
          ^~~
pipes.cpp: In function 'void dfs2(int)':
pipes.cpp:47:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                  ~^~~~~~~~~~~~
pipes.cpp:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0;i<v[x].size();i++)
                      ~^~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:105:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0;i<v[x].size();i++)
                      ~^~~~~~~~~~~~
pipes.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
     ~~~~~^~~~~~~~~~~~~~
pipes.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&a[i]);
         ~~~~~^~~~~~~~~~~~~~
pipes.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 7 ms 2816 KB Output is correct
4 Correct 82 ms 9432 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2864 KB Output is correct
10 Correct 7 ms 2688 KB Output is correct
11 Correct 7 ms 2688 KB Output is correct
12 Correct 7 ms 2816 KB Output is correct
13 Correct 68 ms 8056 KB Output is correct
14 Correct 80 ms 9080 KB Output is correct
15 Correct 87 ms 9560 KB Output is correct
16 Correct 84 ms 8696 KB Output is correct
17 Correct 85 ms 9432 KB Output is correct
18 Correct 86 ms 9432 KB Output is correct
19 Correct 91 ms 8824 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 6 ms 2688 KB Output is correct
22 Correct 85 ms 9432 KB Output is correct
23 Correct 65 ms 8060 KB Output is correct
24 Correct 99 ms 9428 KB Output is correct
25 Correct 70 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 7 ms 2816 KB Output is correct
3 Correct 68 ms 8440 KB Output is correct
4 Correct 59 ms 6776 KB Output is correct
5 Correct 60 ms 6904 KB Output is correct
6 Correct 251 ms 17272 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2688 KB Output is correct
10 Correct 6 ms 2688 KB Output is correct
11 Correct 6 ms 2688 KB Output is correct
12 Correct 6 ms 2688 KB Output is correct
13 Correct 6 ms 2688 KB Output is correct
14 Correct 6 ms 2688 KB Output is correct
15 Correct 6 ms 2816 KB Output is correct
16 Correct 7 ms 2688 KB Output is correct
17 Correct 6 ms 2688 KB Output is correct
18 Correct 6 ms 2688 KB Output is correct
19 Correct 7 ms 2688 KB Output is correct
20 Correct 6 ms 2816 KB Output is correct
21 Correct 7 ms 2816 KB Output is correct
22 Correct 7 ms 2816 KB Output is correct
23 Correct 83 ms 9464 KB Output is correct
24 Correct 103 ms 10744 KB Output is correct
25 Correct 68 ms 8440 KB Output is correct
26 Correct 62 ms 6904 KB Output is correct
27 Correct 70 ms 6648 KB Output is correct
28 Correct 64 ms 7160 KB Output is correct
29 Correct 199 ms 14712 KB Output is correct
30 Correct 98 ms 10104 KB Output is correct
31 Correct 121 ms 11600 KB Output is correct
32 Correct 89 ms 9848 KB Output is correct
33 Correct 76 ms 8616 KB Output is correct
34 Correct 61 ms 6704 KB Output is correct
35 Correct 61 ms 6776 KB Output is correct
36 Correct 75 ms 7032 KB Output is correct
37 Correct 239 ms 17272 KB Output is correct
38 Correct 99 ms 10360 KB Output is correct
39 Correct 88 ms 9720 KB Output is correct
40 Correct 108 ms 10720 KB Output is correct
41 Correct 64 ms 8312 KB Output is correct
42 Correct 61 ms 6648 KB Output is correct
43 Correct 60 ms 6648 KB Output is correct
44 Correct 60 ms 7032 KB Output is correct
45 Correct 198 ms 15480 KB Output is correct
46 Correct 95 ms 9976 KB Output is correct
47 Correct 100 ms 10616 KB Output is correct
48 Correct 106 ms 11384 KB Output is correct
49 Correct 81 ms 8696 KB Output is correct
50 Correct 64 ms 6776 KB Output is correct
51 Correct 61 ms 6904 KB Output is correct
52 Correct 60 ms 6648 KB Output is correct
53 Correct 194 ms 15224 KB Output is correct
54 Correct 100 ms 10104 KB Output is correct