QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#372088 | #6396. Puzzle: Kusabi | Qingyyx | WA | 36ms | 79396kb | C++17 | 4.3kb | 2024-03-30 21:23:34 | 2024-03-30 21:23:36 |
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());
}
if (f[0].com || f[0].q1.size() || f[0].q2.size())return !printf("NO\n");
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: 4ms
memory: 74132kb
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: 11ms
memory: 74180kb
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: 0
Accepted
time: 16ms
memory: 74228kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 36ms
memory: 79396kb
input:
100000 2 1 Duan 3 1 Duan 4 3 - 5 4 Duan 6 3 - 7 4 Duan 8 4 - 9 8 - 10 7 Duan 11 9 - 12 7 Duan 13 7 Duan 14 8 Duan 15 13 - 16 10 Duan 17 11 Duan 18 12 - 19 1 Duan 20 5 Duan 21 4 Duan 22 14 Duan 23 16 - 24 22 Duan 25 16 Duan 26 13 - 27 13 - 28 17 - 29 5 Duan 30 22 - 31 23 - 32 9 Duan 33 5 - 34 30 Duan...
output:
YES 85183 80940 68238 77477 55487 92481 80321 90768 26757 61425 85031 74064 55495 42921 93032 85218 61332 79084 70206 54289 38376 81194 87608 50270 87995 69185 84503 96568 80329 87650 80462 99841 56280 91879 71681 54556 68256 49562 91053 83902 45401 92379 47304 51481 37299 48856 55343 68883 15089 86...
result:
wrong answer Pair 71681-54556 symbol not satisfied.