QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#372086 | #6396. Puzzle: Kusabi | Qingyyx | WA | 16ms | 74284kb | C++17 | 4.3kb | 2024-03-30 21:21:23 | 2024-03-30 21:21:25 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define enl putchar('\n')
#define all(x) (x).begin(),(x).end()
#define debug(x) printf(" "#x":%d\n",x);
using namespace std;
const int MAXN = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
typedef pair<int, int> pii;
char buf[1 << 21], * p1 = buf, * p2 = buf, obuf[1 << 21], * o = obuf, of[35];
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline ll qpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a % mod; n >>= 1; a = a * a % mod; }return res; }
inline int read() { int s = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc())if (c == '-')f = -1; for (; isdigit(c); c = gc())s = s * 10 + c - '0'; return s * f; }
inline void read(int* a, int n) { for (int i = 1; i <= n; ++i)a[i] = read(); }
inline int inal(char* s) { int n = 0; for (s[0] = gc(); !isalpha(s[0]); s[0] = gc()); for (; isalpha(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline void outd(int* a, int n) { for (int i = 1; i <= n; ++i)printf("%d ", a[i]); enl; }
int n, m, q;
struct EG {
int to, nxt;
}e[MAXN << 1];
int head[MAXN], etot;
inline void add(int u, int v) {
e[etot] = EG{ v, head[u] };
head[u] = etot++;
}
void clear(int n = MAXN - 1) { memset(head + 1, -1, sizeof(int) * n); etot = 0; }
int fa[MAXN], dep[MAXN];
vector<pii>ans;
struct FFF {
int com;
struct CMP { bool operator()(int a, int b) { return dep[a] < dep[b]; } };
priority_queue<int, vector<int>, CMP> q1, q2; // 短 长
bool del() {
int s1 = q1.size(), s2 = q2.size();
if (!s1 && !s2)return true;
if (abs(s1 - s2) > 1)return false;
vector<int>v1, v2;
while (!q1.empty())v1.push_back(q1.top()), q1.pop();
while (!q2.empty())v2.push_back(q2.top()), q2.pop();
int rst = -1;
if (s2 > s1)reverse(all(v1)), reverse(all(v2));
for (int i = 0, j = 0; i < s1 && j < s2; ++i, ++j) {
if (dep[v1[i]] >= dep[v2[j]]) {
if (s1 == s2)return false;
if (rst != -1)return false;
if (s1 > s2)rst = v1[i], --j;
else rst = v2[j], --i;
} else {
ans.push_back({ v1[i], v2[j] });
}
}
if (s1 > s2) {
if (rst != -1)q1.push(rst);
else q1.push(v1[s1 - 1]);
} else if (s1 < s2) {
if (rst != -1)q2.push(rst);
else q2.push(v2[s2 - 1]);
}
return true;
}
}f[MAXN];
bool bfs() {
vector<int>path;
queue<int>q;
q.push(1);
while (!q.empty()) {
int u = q.front(); q.pop();
path.push_back(u);
for (int i = head[u]; ~i; i = e[i].nxt) {
q.push(e[i].to);
dep[e[i].to] = dep[u] + 1;
}
}
reverse(all(path));
for (auto u : path) {
if (!f[u].del())return !printf("NO\n");
if (f[u].com && (f[u].q1.size() || f[u].q2.size()))return !printf("NO\n");
if (f[u].com) {
if (f[fa[u]].com)ans.push_back({ f[u].com, f[fa[u]].com }), f[fa[u]].com = 0;
else f[fa[u]].com = f[u].com;
}
if (!f[u].q1.empty())f[fa[u]].q1.push(f[u].q1.top());
if (!f[u].q2.empty())f[fa[u]].q2.push(f[u].q2.top());
}
return true;
}
void solve() {
string s;
cin >> n;
clear(n);
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v >> s;
add(v, u); fa[u] = v;
if (s == "Tong")f[u].com = u;
else if (s == "Duan")f[u].q1.push(u);
else if (s == "Chang")f[u].q2.push(u);
}
if (bfs()) {
printf("YES\n");
for (auto& [x, y] : ans)
printf("%d %d\n", x, y);
}
}
signed main(signed argc, char const* argv[]) {
clock_t c1 = clock();
#ifdef LOCAL
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
//=============================================================
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int TxT = 1;
while (TxT--)
solve();
//=============================================================
#ifdef LOCAL
end :
cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 74176kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 5 4 6 8
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 8ms
memory: 74084kb
input:
10 2 1 Duan 3 2 Duan 4 2 - 5 4 Chang 6 2 Chang 7 1 Duan 8 6 Tong 9 6 Tong 10 3 Chang
output:
YES 9 8 3 10 2 6 7 5
result:
ok Correct.
Test #3:
score: -100
Wrong Answer
time: 7ms
memory: 74284kb
input:
2 2 1 Tong
output:
YES
result:
wrong answer Only odd number of marked vertices to match.