제출 #120827

#제출 시각아이디문제언어결과실행 시간메모리
120827IOrtroiii다리 (APIO19_bridges)C++14
59 / 100
3004 ms13872 KiB
#include <bits/stdc++.h> using namespace std; const int N = 50500; const int M = N + N; const int Q = N + N; const int MAGIC = 320; struct Edge { int l, r; int from, to, lim; Edge(int l = 0, int r = 0, int from = 0, int to = 0, int lim = 0) : l(l), r(r), from(from), to(to), lim(lim) {} bool operator < (const Edge &e) const { return lim > e.lim; } }; vector<Edge> es; struct Query { int from, cost, id; Query(int from = 0, int cost = 0, int id = 0) : from(from), cost(cost), id(id) {} bool operator < (const Query &q) const { return cost > q.cost; } }; vector<Query> qs; int par[N]; int sz[N]; int find(int u) { if (par[u] != u) { par[u] = find(par[u]); } return par[u]; } void merge(int u, int v) { u = find(u); v = find(v); if (u ^ v) { par[v] = u; sz[u] += sz[v]; } } int from[M]; int to[M]; int lim[M]; int last[M]; int op[Q]; int ans[Q]; vector<int> g[N]; bool visit[N]; void dfs(int u, int &ans) { visit[u] = true; ans += sz[u]; for (int v : g[u]) { if (!visit[v]) { dfs(v, ans); } } } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; ++i) { scanf("%d %d %d", from + i, to + i, lim + i); } int q; scanf("%d", &q); for (int i = 1; i <= q; ++i) { scanf("%d", op + i); if (op[i] == 1) { int id, nlim; scanf("%d %d", &id, &nlim); es.emplace_back(last[id], i, from[id], to[id], lim[id]); lim[id] = nlim; last[id] = i; } else { int from, cost; scanf("%d %d", &from, &cost); qs.emplace_back(from, cost, i); } } for (int i = 1; i <= m; ++i) { if (last[i] != q) { es.emplace_back(last[i], q + 1, from[i], to[i], lim[i]); } } sort(es.begin(), es.end()); sort(qs.begin(), qs.end()); for (int l = 0; l < q + 1; l += MAGIC) { int r = min(l + MAGIC, q + 1); vector<Edge> all_es; vector<Edge> cand_es; for (auto e : es) { if (e.l <= l && r <= e.r) { all_es.push_back(e); } else if (e.l < r && l < e.r) { cand_es.push_back(e); } } vector<Query> cqs; for (auto qe : qs) { if (l <= qe.id && qe.id < r) { cqs.push_back(qe); } } for (int i = 1; i <= n; ++i) { par[i] = i; sz[i] = 1; } int ptr = 0; for (auto qe : cqs) { while (ptr < all_es.size() && all_es[ptr].lim >= qe.cost) { merge(all_es[ptr].from, all_es[ptr].to); ptr++; } vector<int> vers; vers.push_back(find(qe.from)); for (auto e : cand_es) { if (e.l <= qe.id && qe.id < e.r && e.lim >= qe.cost) { int u = find(e.from); int v = find(e.to); if (u ^ v) { g[u].push_back(v); g[v].push_back(u); vers.push_back(v); vers.push_back(u); } } } dfs(vers.front(), ans[qe.id]); for (int v : vers) { g[v].clear(); visit[v] = false; } // ans[qe.id] = sz2[find2(qe.from)]; } } for (int i = 1; i <= q; ++i) if (op[i] == 2) { printf("%d\n", ans[i]); } }

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

bridges.cpp: In function 'int main()':
bridges.cpp:123:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
          while (ptr < all_es.size() && all_es[ptr].lim >= qe.cost) {
                 ~~~~^~~~~~~~~~~~~~~
bridges.cpp:73:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &n, &m);
    ~~~~~^~~~~~~~~~~~~~~~~
bridges.cpp:75:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d %d", from + i, to + i, lim + i);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:78:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &q);
    ~~~~~^~~~~~~~~~
bridges.cpp:80:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", op + i);
       ~~~~~^~~~~~~~~~~~~~
bridges.cpp:83:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
          scanf("%d %d", &id, &nlim);
          ~~~~~^~~~~~~~~~~~~~~~~~~~~
bridges.cpp:89:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
          scanf("%d %d", &from, &cost);
          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...