제출 #342946

#제출 시각아이디문제언어결과실행 시간메모리
342946mjhmjh1104Graph (BOI20_graph)C++14
100 / 100
206 ms27624 KiB
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, m, cl[100006];
bool a[100006];
long long c[100006];
bool visited[100006];
vector<pair<int, int>> adj[100006];
vector<double> v[100006];
vector<int> colorList[100006];

int sg(bool x) {
    if (x) return 1;
    else return -1;
}

void dfs(int x, int color) {
    cl[x] = color;
    colorList[color].push_back(x);
    visited[x] = true;
    for (auto &i: adj[x]) {
        if (!visited[i.first]) {
            a[i.first] = !a[x];
            c[i.first] = i.second - c[x];
            dfs(i.first, color);
        } else {
            if (a[i.first] != a[x]) {
                if (c[i.first] + c[x] != i.second) {
                    puts("NO");
                    exit(0);
                }
            } else v[color].push_back((double)(i.second - c[i.first] - c[x]) / (sg(a[i.first]) + sg(a[x])));
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    while (m--) {
        int u, v, c;
        scanf("%d%d%d", &u, &v, &c);
        u--, v--;
        adj[u].push_back({ v, c });
        adj[v].push_back({ u, c });
    }
    a[0] = 1, c[0] = 0;
    int color = 0;
    for (int i = 0; i < n; i++) if (!visited[i]) dfs(i, color++);
    double res[100006] = {};
    for (int i = 0; i < color; i++) {
        if (v[i].empty()) {
            vector<long long> _a;
            for (auto &j: colorList[i]) _a.push_back(-c[j] * sg(a[j]));
            sort(_a.begin(), _a.end());
            for (auto &j: colorList[i]) res[j] = sg(a[j]) * _a[(int)_a.size() / 2] + c[j];
        } else {
            sort(v[i].begin(), v[i].end());
            v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
            if (v[i].size() != 1) {
                puts("NO");
                return 0;
            } else for (auto &j: colorList[i]) res[j] = sg(a[j]) * v[i][0] + c[j];
        }
    }
    puts("YES");
    for (int i = 0; i < n; i++) printf("%.8f ", res[i]);
}

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

Graph.cpp: In function 'int main()':
Graph.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Graph.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |         scanf("%d%d%d", &u, &v, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...