제출 #559764

#제출 시각아이디문제언어결과실행 시간메모리
559764hoanghq2004Pipes (BOI13_pipes)C++14
100 / 100
167 ms35636 KiB
#include <bits/stdc++.h>

using namespace std;

int n, m, vis[100010];
set <int> adj[100010];
int w[100010], s[100010];
map <int, int> res[100010];
vector <int> circle;
pair <int, int> e[500010];

void dfs(int u) {
    vis[u] = 1;
    circle.push_back(u);
    for (auto v: adj[u]) {
        if (vis[v]) continue;
        dfs(v);
    }
}

int main() {
    scanf("%d%d", &n, &m);
    if (m > n) {
        printf("0\n");
        exit(0);
    }
    for (int i = 1; i <= n; ++i) scanf("%d", &w[i]);
    for (int i = 1; i <= m; ++i) {
        int u, v, w;
        scanf("%d%d", &u, &v);
        adj[u].insert(v);
        adj[v].insert(u);
        e[i] = {u, v};
    }
    queue <int> q;
    for (int i = 1; i <= n; ++i)
        if (adj[i].size() == 1) q.push(i);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        int p = *adj[u].begin();
        res[u][p] = res[p][u] = w[u];
        w[p] -= w[u];
        adj[p].erase(u);
        adj[u].clear();
        if (adj[p].size() == 1) q.push(p);
    }
    int root = 0;
    for (int i = 1; i <= n; ++i)
        if (adj[i].size()) root = i;
    dfs(root);
    if (!circle.empty() && circle.size() % 2 == 0) {
        printf("0\n");
        exit(0);
    }
    for (int i = 0; i < circle.size(); ++i) s[i + 1] = (s[i]) + (i % 2 == 0 ? 1 : -1) * w[circle[i]];
    for (int i = 0; i < circle.size(); ++i) {
        int u = circle[i], v = circle[(i + 1) % circle.size()];
        res[u][v] = res[v][u] = (s[i + 1] - s[circle.size()] + s[i + 1]) * (i % 2 == 0 ? 1 : -1) / 2;
    }
    for (int i = 1; i <= m; ++i) {
        int u = e[i].first, v = e[i].second;
        printf("%d\n", res[u][v] * 2);
    }
}

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

pipes.cpp: In function 'int main()':
pipes.cpp:29:19: warning: unused variable 'w' [-Wunused-variable]
   29 |         int u, v, w;
      |                   ^
pipes.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for (int i = 0; i < circle.size(); ++i) s[i + 1] = (s[i]) + (i % 2 == 0 ? 1 : -1) * w[circle[i]];
      |                     ~~^~~~~~~~~~~~~~~
pipes.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 0; i < circle.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~~
pipes.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:27:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     for (int i = 1; i <= n; ++i) scanf("%d", &w[i]);
      |                                  ~~~~~^~~~~~~~~~~~~
pipes.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...