Submission #479402

# Submission time Handle Problem Language Result Execution time Memory
479402 2021-10-11T15:41:47 Z DDTerziev04 Graph (BOI20_graph) C++14
58 / 100
92 ms 15952 KB
#include<iostream>
#include<vector>
using namespace std;

const int MAXN=1e5;
const double INF=1e9;

vector<pair<int, int> > adj[MAXN];
vector<int> seg;
double val[MAXN], ans[MAXN];
bool v[MAXN], s[MAXN];

void DFS(int idx)
{
   v[idx]=true;
   seg.push_back(idx);

   for(pair<int, int> node:adj[idx])
   {
      if(!v[node.first])
      {
         val[node.first]=node.second-val[idx];
         s[node.first]=!s[idx];

         DFS(node.first);
      }
   }

   return;
}

double GetValue(double x1, bool s1, double x2, bool s2)
{
   if(s1==s2)
   {
      if(x1-x2==0)
      {
         return INF;
      }

      return -INF;
   }

   if(s1)
   {
      return -(x1-x2)/2;
   }

   return (x1-x2)/2;
}

double GetSum(double x)
{
   double res=0;
   for(int node:seg)
   {
      if(s[node])
      {
         res+=abs(val[node]+x);
      }
      else
      {
         res+=abs(val[node]-x);
      }
   }

   return res;
}

bool AssignValues(int idx)
{
   seg.clear();

   val[idx]=0;
   s[idx]=true;

   DFS(idx);

   double x=INF;
   for(int node:seg)
   {
      for(pair<int, int> u:adj[node])
      {
         double temp=GetValue(val[u.first], s[u.first], u.second-val[node], !s[node]);

         if(temp==-INF)
         {
            return false;
         }

         if(temp==INF)
         {
            continue;
         }

         if(x==INF)
         {
            x=temp;
         }
         else if(x!=temp)
         {
            return false;
         }
      }
   }

   if(x==INF)
   {
      double res=INF;
      for(double i=-10; i<10; i+=0.5)
      {
         if(GetSum(i)<res)
         {
            x=i;
            res=GetSum(i);
         }
      }
   }

   for(int node:seg)
   {
      if(s[node])
      {
         ans[node]=val[node]+x;
      }
      else
      {
         ans[node]=val[node]-x;
      }
   }

   return true;
}

int main()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);

   int n, m;
   cin >> n >> m;

   for(int i=0; i<m; i++)
   {
      int a, b, x;
      cin >> a >> b >> x;
      a--, b--;

      adj[a].push_back({b, x});
      adj[b].push_back({a, x});
   }

   for(int i=0; i<n; i++)
   {
      val[i]=0;
      v[i]=false;
   }

   for(int i=0; i<n; i++)
   {
      if(!v[i] && !AssignValues(i))
      {
         cout << "NO\n";

         return 0;
      }
   }

   cout << "YES\n";
   for(int i=0; i<n; i++)
   {
      cout << ans[i] << " ";
   }
   cout << "\n";

   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2636 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2764 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 3 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2636 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 1 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 2 ms 2636 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2636 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2764 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 3 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2636 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 1 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 2 ms 2636 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2636 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2636 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2596 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2636 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2764 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 3 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2636 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 1 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 2 ms 2636 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2636 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2636 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2596 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
37 Correct 2 ms 2636 KB answer = YES
38 Correct 2 ms 2636 KB answer = YES
39 Correct 2 ms 2636 KB answer = YES
40 Correct 3 ms 2764 KB answer = YES
41 Correct 2 ms 2636 KB answer = NO
42 Correct 3 ms 2764 KB answer = YES
43 Correct 2 ms 2764 KB answer = YES
44 Correct 3 ms 2636 KB answer = YES
45 Correct 3 ms 2764 KB answer = YES
46 Correct 2 ms 2636 KB answer = YES
47 Correct 2 ms 2764 KB answer = YES
48 Correct 3 ms 2684 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2636 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2764 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 3 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2636 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 1 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 2 ms 2636 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2636 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2636 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2596 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
37 Correct 2 ms 2636 KB answer = YES
38 Correct 2 ms 2636 KB answer = YES
39 Correct 2 ms 2636 KB answer = YES
40 Correct 3 ms 2764 KB answer = YES
41 Correct 2 ms 2636 KB answer = NO
42 Correct 3 ms 2764 KB answer = YES
43 Correct 2 ms 2764 KB answer = YES
44 Correct 3 ms 2636 KB answer = YES
45 Correct 3 ms 2764 KB answer = YES
46 Correct 2 ms 2636 KB answer = YES
47 Correct 2 ms 2764 KB answer = YES
48 Correct 3 ms 2684 KB answer = YES
49 Correct 12 ms 3404 KB answer = YES
50 Correct 11 ms 3788 KB answer = YES
51 Correct 11 ms 3768 KB answer = YES
52 Correct 5 ms 3576 KB answer = NO
53 Correct 2 ms 2760 KB answer = YES
54 Correct 4 ms 2764 KB answer = YES
55 Correct 6 ms 3016 KB answer = YES
56 Correct 11 ms 3360 KB answer = YES
57 Correct 11 ms 3276 KB answer = YES
58 Correct 10 ms 3404 KB answer = YES
59 Correct 9 ms 3276 KB answer = YES
60 Correct 11 ms 3332 KB answer = YES
61 Correct 8 ms 3068 KB answer = YES
62 Correct 58 ms 10132 KB answer = NO
63 Correct 64 ms 10212 KB answer = YES
64 Correct 58 ms 10168 KB answer = NO
65 Correct 63 ms 10232 KB answer = YES
66 Correct 3 ms 2764 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2636 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2764 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 3 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2636 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 1 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 2 ms 2636 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2636 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2636 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2596 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
37 Correct 2 ms 2636 KB answer = YES
38 Correct 2 ms 2636 KB answer = YES
39 Correct 2 ms 2636 KB answer = YES
40 Correct 3 ms 2764 KB answer = YES
41 Correct 2 ms 2636 KB answer = NO
42 Correct 3 ms 2764 KB answer = YES
43 Correct 2 ms 2764 KB answer = YES
44 Correct 3 ms 2636 KB answer = YES
45 Correct 3 ms 2764 KB answer = YES
46 Correct 2 ms 2636 KB answer = YES
47 Correct 2 ms 2764 KB answer = YES
48 Correct 3 ms 2684 KB answer = YES
49 Correct 12 ms 3404 KB answer = YES
50 Correct 11 ms 3788 KB answer = YES
51 Correct 11 ms 3768 KB answer = YES
52 Correct 5 ms 3576 KB answer = NO
53 Correct 2 ms 2760 KB answer = YES
54 Correct 4 ms 2764 KB answer = YES
55 Correct 6 ms 3016 KB answer = YES
56 Correct 11 ms 3360 KB answer = YES
57 Correct 11 ms 3276 KB answer = YES
58 Correct 10 ms 3404 KB answer = YES
59 Correct 9 ms 3276 KB answer = YES
60 Correct 11 ms 3332 KB answer = YES
61 Correct 8 ms 3068 KB answer = YES
62 Correct 58 ms 10132 KB answer = NO
63 Correct 64 ms 10212 KB answer = YES
64 Correct 58 ms 10168 KB answer = NO
65 Correct 63 ms 10232 KB answer = YES
66 Correct 3 ms 2764 KB answer = YES
67 Incorrect 92 ms 15952 KB participant answer is larger than the answer of jury
68 Halted 0 ms 0 KB -