結果
| 問題 |
No.922 東北きりきざむたん
|
| コンテスト | |
| ユーザー |
🍮かんプリン
|
| 提出日時 | 2019-11-08 22:39:57 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,198 bytes |
| コンパイル時間 | 1,679 ms |
| コンパイル使用メモリ | 178,540 KB |
| 実行使用メモリ | 11,520 KB |
| 最終ジャッジ日時 | 2024-09-15 01:52:13 |
| 合計ジャッジ時間 | 6,023 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 3 |
| other | AC * 3 WA * 23 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:59:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
59 | int u, v; scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:68:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
68 | int a, b; scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include "bits/stdc++.h"
#define REP(i, n) for(int i = 0; i < int(n); i++)
#define FOR(i,n,m) for(int i = int(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 6;
const ll LLINF = 1e18 + 1;
// UnionFind
class UnionFind {
private:
vector<int> par;
public:
UnionFind(int n) {
par.resize(n, -1);
}
int root(int x) {
if (par[x] < 0) return x;
return par[x] = root(par[x]);
}
bool unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry) return false;
if (size(rx) < size(ry)) swap(rx, ry);
par[rx] += par[ry];
par[ry] = rx;
return true;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) {
return -par[root(x)];
}
};
ll dfs(int v,int depth, const vector<vector<int>> &G, vector<int> &cnt) {
ll res = (ll)cnt[v] * depth;
for (auto u : G[v]) {
res += dfs(u, depth+1,G, cnt);
}
return res;
}
int main() {
int n, m, q; cin >> n >> m >> q;
UnionFind uni(n);
vector<vector<int>> G(n);
REP(i, m) {
int u, v; scanf("%d %d", &u, &v);
u--; v--;
uni.unite(u,v);
G[u].push_back(v);
G[v].push_back(u);
}
vector<int> cnt(n);
map<int, int> mp;
REP(i, q) {
int a, b; scanf("%d %d", &a, &b);
a--; b--;
if (uni.same(a, b)) continue;
cnt[a]++;
cnt[b]++;
if (cnt[mp[uni.root(a)]] < cnt[a]) {
mp[uni.root(a)] = a;
}
if (cnt[mp[uni.root(b)]] < cnt[b]) {
mp[uni.root(b)] = b;
}
}
ll ans = 0;
struct ttt {
int a, b, c;
};
for (auto p : mp) {
queue<ttt> que;
que.push({p.second, -1, 0});
while (!que.empty()) {
auto pp = que.front(); que.pop();
ans += (ll)cnt[pp.a] * pp.c;
for (auto v : G[pp.a]) {
if (v == pp.b) continue;
que.push({v,pp.a,pp.c + 1});
}
}
}
cout << ans << endl;
return 0;
}
🍮かんプリン