QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#689470 | #6396. Puzzle: Kusabi | MiniLong | WA | 2ms | 8696kb | C++14 | 4.5kb | 2024-10-30 17:14:10 | 2024-10-30 17:14:10 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e5 + 5;
int n, id, dfn[N], siz[N], dep[N], c[N];//1:long 2:short 3:same
int f[N];
vector<PII> ans;
vector<int> G[N];
void fail(){puts("NO"); exit(0);}
void dfs(int u){
vector<PII> cur[4];
for(auto &v : G[u]){
dep[v] = dep[u] + 1, dfs(v);
if(f[v]) cur[c[f[v]]].pb({dep[f[v]], f[v]});
}
if(c[u]) cur[c[u]].pb({dep[u], u});
_rep(i, 1, 3) sort(cur[i].begin(), cur[i].end());
vector<PII> now;
while(cur[3].size()){
if(cur[3].size() == 1) now.pb(cur[3].back()), cur[3].pop_back();
else{
int len = cur[3].size();
if(cur[3][len - 1].fi == cur[3][len - 2].fi){
ans.pb({cur[3][len - 1].se, cur[3][len - 2].se});
cur[3].pop_back(), cur[3].pop_back();
}else{
now.pb(cur[3].back());
cur[3].pop_back();
}
}
}
if(cur[1].size() > cur[2].size()){
reverse(cur[1].begin(), cur[1].end());
reverse(cur[2].begin(), cur[2].end());
while(cur[1].size() && cur[2].size()){
int u = cur[1].back().se, v = cur[2].back().se;
if(dep[u] > dep[v]){
ans.pb({u, v}), cur[1].pop_back(), cur[2].pop_back();
continue;
}
now.pb(cur[1].back());
cur[1].pop_back();
}
}else{
while(cur[1].size() && cur[2].size()){
int u = cur[1].back().se, v = cur[2].back().se;
if(dep[u] > dep[v]){
ans.pb({u, v}), cur[1].pop_back(), cur[2].pop_back();
continue;
}
now.pb(cur[2].back());
cur[2].pop_back();
}
}
for(auto &i : cur[1]) now.pb(i); for(auto &i : cur[2]) now.pb(i);
if(now.size() > 1) fail();
if(now.size() == 1) f[u] = now[0].se;
}
int main(){
read(n);
_rep(i, 2, n){
int u, v; read(u, v);
char ch[10]; scanf("%s", ch);
G[v].pb(u);
if(ch[0] == '-') c[u] = 0;
else if(ch[0] == 'C') c[u] = 1;
else if(ch[0] == 'D') c[u] = 2;
else c[u] = 3;
}
dfs(1);
puts("YES");
for(auto &i : ans) write(i.fi, i.se), puts("");
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 8696kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 5 4 8 6
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 1ms
memory: 8152kb
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 10 3 9 8 6 2 5 7
result:
ok Correct.
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 7972kb
input:
2 2 1 Tong
output:
YES
result:
wrong answer Only odd number of marked vertices to match.