답안 #479403

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
479403 2021-10-11T15:42:25 Z DDTerziev04 Graph (BOI20_graph) C++14
58 / 100
103 ms 14588 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=-20; i<20; 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;
}
# 결과 실행 시간 메모리 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 2636 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 2 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 2 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 3 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 1 ms 2636 KB answer = NO
23 Correct 1 ms 2636 KB answer = NO
# 결과 실행 시간 메모리 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 2636 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 2 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 2 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 3 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 1 ms 2636 KB answer = NO
23 Correct 1 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 2568 KB answer = YES
32 Correct 3 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2568 KB answer = YES
# 결과 실행 시간 메모리 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 2636 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 2 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 2 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 3 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 1 ms 2636 KB answer = NO
23 Correct 1 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 2568 KB answer = YES
32 Correct 3 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2568 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 2636 KB answer = YES
41 Correct 3 ms 2636 KB answer = NO
42 Correct 2 ms 2636 KB answer = YES
43 Correct 3 ms 2636 KB answer = YES
44 Correct 2 ms 2636 KB answer = YES
45 Correct 3 ms 2636 KB answer = YES
46 Correct 2 ms 2636 KB answer = YES
47 Correct 3 ms 2764 KB answer = YES
48 Correct 3 ms 2764 KB answer = YES
# 결과 실행 시간 메모리 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 2636 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 2 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 2 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 3 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 1 ms 2636 KB answer = NO
23 Correct 1 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 2568 KB answer = YES
32 Correct 3 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2568 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 2636 KB answer = YES
41 Correct 3 ms 2636 KB answer = NO
42 Correct 2 ms 2636 KB answer = YES
43 Correct 3 ms 2636 KB answer = YES
44 Correct 2 ms 2636 KB answer = YES
45 Correct 3 ms 2636 KB answer = YES
46 Correct 2 ms 2636 KB answer = YES
47 Correct 3 ms 2764 KB answer = YES
48 Correct 3 ms 2764 KB answer = YES
49 Correct 17 ms 3284 KB answer = YES
50 Correct 11 ms 3660 KB answer = YES
51 Correct 12 ms 3544 KB answer = YES
52 Correct 5 ms 3540 KB answer = NO
53 Correct 3 ms 2636 KB answer = YES
54 Correct 4 ms 2764 KB answer = YES
55 Correct 8 ms 2976 KB answer = YES
56 Correct 12 ms 3276 KB answer = YES
57 Correct 11 ms 3148 KB answer = YES
58 Correct 10 ms 3148 KB answer = YES
59 Correct 9 ms 3268 KB answer = YES
60 Correct 12 ms 3292 KB answer = YES
61 Correct 6 ms 3020 KB answer = YES
62 Correct 63 ms 7928 KB answer = NO
63 Correct 72 ms 8004 KB answer = YES
64 Correct 63 ms 7876 KB answer = NO
65 Correct 76 ms 8004 KB answer = YES
66 Correct 4 ms 2764 KB answer = YES
# 결과 실행 시간 메모리 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 2636 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2636 KB answer = YES
11 Correct 2 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 2 ms 2636 KB answer = YES
16 Correct 2 ms 2636 KB answer = YES
17 Correct 3 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 1 ms 2636 KB answer = NO
23 Correct 1 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 2568 KB answer = YES
32 Correct 3 ms 2636 KB answer = YES
33 Correct 2 ms 2636 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2636 KB answer = YES
36 Correct 2 ms 2568 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 2636 KB answer = YES
41 Correct 3 ms 2636 KB answer = NO
42 Correct 2 ms 2636 KB answer = YES
43 Correct 3 ms 2636 KB answer = YES
44 Correct 2 ms 2636 KB answer = YES
45 Correct 3 ms 2636 KB answer = YES
46 Correct 2 ms 2636 KB answer = YES
47 Correct 3 ms 2764 KB answer = YES
48 Correct 3 ms 2764 KB answer = YES
49 Correct 17 ms 3284 KB answer = YES
50 Correct 11 ms 3660 KB answer = YES
51 Correct 12 ms 3544 KB answer = YES
52 Correct 5 ms 3540 KB answer = NO
53 Correct 3 ms 2636 KB answer = YES
54 Correct 4 ms 2764 KB answer = YES
55 Correct 8 ms 2976 KB answer = YES
56 Correct 12 ms 3276 KB answer = YES
57 Correct 11 ms 3148 KB answer = YES
58 Correct 10 ms 3148 KB answer = YES
59 Correct 9 ms 3268 KB answer = YES
60 Correct 12 ms 3292 KB answer = YES
61 Correct 6 ms 3020 KB answer = YES
62 Correct 63 ms 7928 KB answer = NO
63 Correct 72 ms 8004 KB answer = YES
64 Correct 63 ms 7876 KB answer = NO
65 Correct 76 ms 8004 KB answer = YES
66 Correct 4 ms 2764 KB answer = YES
67 Incorrect 103 ms 14588 KB participant answer is larger than the answer of jury
68 Halted 0 ms 0 KB -