QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#386572 | #6396. Puzzle: Kusabi | CRN2010 | WA | 25ms | 10156kb | C++20 | 4.2kb | 2024-04-11 18:19:30 | 2024-04-11 18:19:32 |
Judging History
answer
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector")
#include <bits/stdc++.h>
using std::cin ;
using std::cout ;
const int inf = 0x3f3f3f3f ;
const long long llinf = 0x3f3f3f3f3f3f3f3f ;
const int N = 1e5 + 10 ;
int n ;
std::vector<int> edges[N] ;
int d[N] , color[N] , idx[N] ;
std::vector<std::pair<int , int>> ret ;
void dfs(int v,int f,int dist) {
d[v] = dist ;
std::vector<int> c1 , c2 , c3 ;
if ( color[v] == 1 ) c1.push_back(v) ;
else if ( color[v] == 2 ) c2.push_back(v) ;
else if ( color[v] == 3 ) c3.push_back(v) ;
color[v] = 0 ;
for( auto u : edges[v] ) {
if ( u != f ) {
dfs(u , v , dist + 1) ;
if ( color[idx[u]] == 1 ) c1.push_back(u) ;
else if ( color[idx[u]] == 2 ) c2.push_back(u) ;
else if ( color[idx[u]] == 3 ) c3.push_back(u) ;
}
}
int x = c3.size() ;
for(int i = 0 ; i < c3.size() ; ++ i) {
if ( c3[i] == -1 ) continue ;
for(int j = i + 1 ; j < c3.size() ; ++ j)
if ( c3[j] != -1 && d[idx[c3[i]]] == d[idx[c3[j]]] ) {
ret.push_back({idx[c3[i]] , idx[c3[j]]}) ;
x -= 2 ;
c3[j] = c3[i] = -1 ;
break ;
}
}
if ( x > 1 ) {
cout << "NO\n" ;
exit(0) ;
}
else if ( x == 1 ) {
for(auto p : c3 ) if ( p != -1 ) {
d[v] = d[p] ;
color[v] = 3 ;
idx[v] = p ;
break ;
}
}
int y1 = c1.size() , y2 = c2.size() ;
for(int i = 0 ; i < c1.size() ; ++ i) {
int id = -1 ;
for(int j = 0 ; j < c2.size() ; ++ j) {
if ( d[idx[c1[i]]] < d[idx[c2[j]]] ) {
if ( id == -1 ) id = idx[c2[j]] ;
else if ( d[idx[c2[j]]] < d[id] ) id = idx[c2[j]] ;
}
}
if ( id != -1 ) {
--y1 , --y2 ;
ret.push_back({idx[c1[i]] , id}) ;
d[id] = -1;
d[idx[c1[i]]] = -1;
}
}
if ( y1 + y2 > 1 ) {
cout << y1 << ' ' << y2 << '\n' ;
for(auto p : c1) {
if ( d[p] != -1 ) {
cout << p << ' ' << d[p] << ' ' << idx[p] << '\n' ;
break ;
}
}
for(auto p : c2 ) {
if ( d[p] != -1 ) {
cout << p << ' ' << d[p] << ' ' << idx[p] << '\n' ;
break ;
}
}
cout << "N1O\n" ;
exit(0) ;
}
else if ( y1 + y2 == 1 && color[v] != 0 ) {
cout << "NO\n" ;
exit(0) ;
}
else if ( y1 ) {
for(auto p : c1) if ( d[p] != -1 ) {
d[v] = d[idx[p]] ;
color[v] = 1 ;
idx[v] = idx[p];
break ;
}
}
else if ( y2 ) {
for(auto p : c2) if ( d[p] != -1 ) {
d[v] = d[idx[p]] ;
color[v] = 2 ;
idx[v] = idx[p] ;
break ;
}
}
}
void solve()
{
cin >> n ;
idx[n] = n ;
for(int i = 1 ; i < n ; ++ i)
{
idx[i] = i ;
int u , v ;
std::string col ;
cin >> u >> v >> col ;
if ( col == "Duan" ) color[u] = 1 ;
else if ( col == "Chang" ) color[u] = 2 ;
else if ( col == "Tong" ) color[u] = 3 ;
edges[u].push_back(v) ;
edges[v].push_back(u) ;
}
dfs(1 , 0 , 0) ;
if ( color[1] ) return cout << "NO\n" , void() ;
cout << "YES\n" ;
for(auto p : ret) cout << p.first << ' ' << p.second << '\n' ;
// for(int i = 1 ; i <= n ; ++ i)
// {
// cout << i << " : " << color[i] << ' ' << d[i] << ' ' << idx[i] << '\n' ;
// }
}
int main() {
std::ios::sync_with_stdio(false) ;
std::cin.tie(0) ;
std::cout.tie(0) ;
#ifdef LOCAL
freopen("D:\\Codes\\in.txt" , "r" , stdin) ;
freopen("D:\\Codes\\out.txt" , "w" , stdout) ;
#endif
// cout << std::fixed << std::setprecision(20) ;
int _ = 1 ;
// cin >> _;
while (_--) {
solve() ;
}
return 0 ;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3668kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 4 5 6 8
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 1ms
memory: 3564kb
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 3 10 8 9 2 6 7 5
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
2 2 1 Tong
output:
NO
result:
ok Correct.
Test #4:
score: -100
Wrong Answer
time: 25ms
memory: 10156kb
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:
1 1 10902 5 22475 48783 5 48783 N1O
result:
wrong answer YES or NO expected in answer, but 1 found.