QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#861592 | #9975. Hitoshizuku | ucup-team6275# | RE | 0ms | 0kb | C++23 | 2.9kb | 2025-01-18 18:26:41 | 2025-01-18 18:26:41 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (n); i += 1)
#define len(a) ((int)(a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rnd(324);
const ll inf = 1e18;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> gr(n);
vector<ll> dist(n, inf);
vector<int> ans(m, -1);
map<pair<int, int>, int> inds;
dist[0] = 1;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
gr[u].push_back(v);
gr[v].push_back(u);
inds[{u, v}] = i;
inds[{v, u}] = i;
}
int k;
cin >> k;
vector<vector<int>> cuts;
vector<int> marks(n);
for (int i = 0; i < k; ++i) {
int t;
cin >> t;
vector<int> cur(t);
for (auto &c : cur) {
cin >> c;
--c;
++marks[c];
}
sort(all(cur));
cuts.push_back(cur);
}
sort(all(cuts));
priority_queue<pair<ll, int>> q;
vector<int> used(n);
q.push({0, 0});
bool flag = true;
while (true) {
vector<int> cur_bad;
ll max_dist = 0;
while (!q.empty()) {
int v = q.top().second;
q.pop();
if (used[v])
continue;
used[v] = 1;
max_dist = max(max_dist, dist[v]);
for (auto to : gr[v]) {
if (marks[to] == 0 && dist[to] > dist[v] + 1) {
ans[inds[{to, v}]] = 1;
dist[to] = dist[v] + 1;
q.push({dist[to], to});
} else if (marks[to] > 0) {
cur_bad.push_back(to);
max_dist = max(max_dist, dist[v] + 1);
}
}
}
sort(all(cur_bad));
cur_bad.erase(unique(all(cur_bad)), cur_bad.end());
auto it = lower_bound(all(cuts), cur_bad);
if (it == cuts.end() || (*it) != cur_bad) {
flag = false;
break;
}
for (auto c : cur_bad) {
marks[c]--;
if (marks[c] == 0) {
for (auto to : gr[c]) {
if (used[to]) {
ans[inds[{c, to}]] = max_dist - dist[to];
assert(max_dist - dist[to] > 0);
}
}
q.push({c, dist[c]});
}
}
}
if (flag) {
cout << "Yes\n";
for (auto c : ans) {
cout << max(c, 1) << " ";
}
cout << "\n";
} else {
cout << "No\n";
}
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
2 2 1 2 2 2 2 3 3 5 4 4 4 5 1 1 1 1 1000000000 1000000000 1000000000