제출 #340185

#제출 시각아이디문제언어결과실행 시간메모리
340185blueGraph (BOI20_graph)C++11
100 / 100
421 ms27232 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

/*
- Values in different connected components are independent of each other.
- When DFS'ing a CC, the starting vertex has a value of x
- For every edge (a, b, c)
    If b is unexplored, value[b] = c - value[a]
    If b is explored, but the equation c = value[a] + value[b] gives no additional information, do nothing
    If b is explored, and the equation gives additional information, use it to compute x.
    Check for contradictions later.
*/

int N, M;
vector<int> edge[100001];
vector<int> color[100001];

vector<double> x_coeff(100001, 0.0);
vector<double> c_coeff(100001, 0.0);

vector<int> cc(100001, 0);
int cc_count = 0;
vector<double> xval(1);

vector<double> findx;

void dfs(int u)
{
    findx.push_back(c_coeff[u] * x_coeff[u] * -1.0);
    for(int i = 0; i < edge[u].size(); i++)
    {
        int v = edge[u][i];
        int c = color[u][i];

        if(!cc[v])
        {
            cc[v] = cc_count;
            x_coeff[v] = -1.0 * x_coeff[u];
            c_coeff[v] = double(c) - c_coeff[u];
            dfs(v);
        }
        else
        {
            if(x_coeff[u] + x_coeff[v] == 0.0) continue;
            xval.back() = (double(c) - (c_coeff[u] + c_coeff[v]))/(x_coeff[u] + x_coeff[v]);
        }
    }
}

int main()
{
    int N, M;
    cin >> N >> M;

    int a, b, c;
    for(int i = 1; i <= M; i++)
    {
        cin >> a >> b >> c;

        edge[a].push_back(b);
        color[a].push_back(c);

        edge[b].push_back(a);
        color[b].push_back(c);
    }

    for(int i = 1; i <= N; i++)
    {
        if(cc[i]) continue;
        cc_count++;
        cc[i] = cc_count;
        x_coeff[i] = 1.0;
        xval.push_back(-9999.9);
        findx.clear();
        dfs(i);
        if(xval.back() == -9999.9)
        {
            sort(findx.begin(), findx.end());
            xval.back() = findx[findx.size()/2];
        }
    }

    double val[N+1];
    for(int i = 1; i <= N; i++) val[i] = x_coeff[i] * xval[cc[i]] + c_coeff[i];
    for(int i = 1; i <= N; i++)
    {
        for(int j = 0; j < edge[i].size(); j++)
        {
            if(val[i] + val[edge[i][j]] != color[i][j])
            {
                cout << "NO\n";
                return 0;
            }
        }
    }
    cout << "YES\n";
    for(int i = 1; i <= N; i++) cout << val[i] << ' ';
    cout << '\n';
}

컴파일 시 표준 에러 (stderr) 메시지

Graph.cpp: In function 'void dfs(int)':
Graph.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(int i = 0; i < edge[u].size(); i++)
      |                    ~~^~~~~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j = 0; j < edge[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...