QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#351540 | #6559. A Tree and Two Edges | janY | WA | 41ms | 9216kb | C++20 | 6.5kb | 2024-03-12 01:25:10 | 2024-03-12 01:25:10 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define ld long double
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define rev(x) reverse(x.begin(), x.end())
#define fi first
#define se second
#define pb push_back
#define PI 3.14159265359
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
#define sortpairbysec(x) sort(all(x), sortbysec)
bool sortcond(const pair<int,int> &a,const pair<int,int> &b){
if (a.fi != b.fi){
return a.fi < b.fi;
} else {
return a.se > b.se;
}
}
struct myComp {
constexpr bool operator()( pii const& a, pii const& b) const noexcept
{
if (a.first != b.first){
return a.first < b.first;
} else {
return a.second > b.second;
}
}
};
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
uniform_int_distribution<ll> uid(0,lim-1);
return uid(rang);
}
const int mod = 998244353;
const int N = 3e5, M = N;
const ll inf = 1e18;
//=======================
struct disjSet { // Disjoint set
int *rank, *parent, n;
disjSet() {}
disjSet(int n) {init(n);}
void init(int n){
rank = new int[n];
parent = new int[n];
this->n = n;
for (int i = 0; i < n; i++) {
rank[i] = 0;
parent[i] = i;
}
}
int find(int a) {
if (parent[a] != a){
//return find(parent[a]); // no path compression
parent[a] = find(parent[a]); // path compression
}
return parent[a];
}
bool Union(int a, int b) {
int a_set = find(a);
int b_set = find(b);
if (a_set == b_set) return false;
if (rank[a_set] < rank[b_set]) {
update_union(a_set, b_set);
} else if (rank[a_set] > rank[b_set]) {
update_union(b_set, a_set);
} else {
update_union(b_set, a_set);
rank[a_set] = rank[a_set] + 1;
}
return true;
}
// change merge behaviour here
void update_union(int a, int b){ // merge a into b
parent[a] = b;
}
};
//=======================
// & - bitwise AND; | - bitwise OR; ^ - bitwise XOR
// cout.precision(7);
// next_permutation(arr.begin(), arr.end());
// long long long long long long long long long long long long long long long long long long long long long long long long long long long
// priority_queue<int, vector<int>, greater<int>> a; (min heap)
// ll hash1 = hash<string>{}(to_string(1));
// always lower_bound
// __builtin_clz(n) count leading zeros
vl a;
ll n, m, k, q;
vector<vector<bool>> ans;
vpii xtra;
vvi g;
vector<pii> is_good;
vector<vpii> que;
vi blocked;
void fill_subtree(int loc, int p, int val, bool stop_block){
if (blocked[loc] && stop_block) return;
is_good[loc] = {val, p};
for (auto &i : g[loc]){
if (i == p || blocked[i]) continue;
fill_subtree(i, loc, val, 1);
}
}
void walk(int loc, int p, int typ){
if (blocked[loc]) return;
blocked[loc] = 1;
pii rem = {-1, -1};
vi sav;
if (is_good[loc].fi == 1){
rem = is_good[loc];
for (auto &i : g[loc]){
if (i == p || i == rem.se) continue;
if (is_good[i].fi == 1){
fill_subtree(i, loc, 0, 1);
sav.pb(i);
}
}
is_good[loc] = {0, 0};
}
for (auto &i : que[loc]){
if (is_good[i.fi].fi == 1) ans[typ][i.se] = 1;
}
for (auto &i : g[loc]){
if (i == p) continue;
walk(i, loc, typ);
}
if (rem != (pii){-1, -1}){
is_good[loc] = rem;
for (auto &i : sav){
fill_subtree(i, loc, 1, 1);
}
}
blocked[loc] = 0;
}
bool bad_path;
bool block_path(int loc, int p, int targ){
if (blocked[loc]) {
bad_path = 1;
return 0;
}
if (loc == targ){
blocked[loc] = 1;
return 1;
}
for (auto &i : g[loc]){
if (i == p) continue;
if (block_path(i, loc, targ)){
blocked[loc] = 1;
return 1;
}
}
return 0;
}
void test(int typ){
blocked.assign(n, 0);
is_good.assign(n, {0, 0});
if (typ == 2){
blocked[xtra[0].fi] = 1;
bad_path = 0;
block_path(xtra[0].se, xtra[0].se, xtra[1].fi);
if (bad_path) return;
fill_subtree(xtra[0].fi, xtra[0].fi, 1, 0);
walk(xtra[1].se, xtra[1].se, typ);
return;
}
pii the_edge;
if (typ == 0) the_edge = xtra[0];
if (typ == 1) the_edge = xtra[1];
fill_subtree(the_edge.se, the_edge.se, 1, 0);
blocked[the_edge.se] = 1;
walk(the_edge.fi, the_edge.fi, typ);
}
void solve(int tc) {
int i, j;
cin >> n >> q;
ans.resize(3, vector<bool>(q, 0));
disjSet dsu(n);
g.resize(n);
fo(i, n+1){
int u, v;
cin >> u >> v;
u--; v--;
if (dsu.Union(u, v)){
g[u].pb(v);
g[v].pb(u);
} else {
xtra.pb({u, v});
}
}
que.resize(n);
fo(i, q){
int u, v;
cin >> u >> v;
u--; v--;
que[u].pb({v, i});
}
test(0);
swap(xtra[0].fi, xtra[0].se);
test(0);
test(1);
swap(xtra[1].fi, xtra[1].se);
test(1);
test(2);
swap(xtra[0].fi, xtra[0].se);
test(2);
swap(xtra[1].fi, xtra[1].se);
test(2);
swap(xtra[0].fi, xtra[0].se);
test(2);
swap(xtra[0], xtra[1]);
test(2);
swap(xtra[0].fi, xtra[0].se);
test(2);
swap(xtra[1].fi, xtra[1].se);
test(2);
swap(xtra[0].fi, xtra[0].se);
test(2);
fo(i, q){
cout << 1+ans[0][i]+ans[1][i]+ans[2][i] << "\n";
// cout << ans[0][i] << " " << ans[1][i] << " " << ans[2][i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
// cin >> t;
int i;
fo(i, t) {
solve(i+1);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3652kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
3 3 3 3 3 4
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
6 4 1 2 1 3 1 6 2 3 3 4 3 5 4 5 1 2 1 3 1 4 1 6
output:
2 2 4 1
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 41ms
memory: 9216kb
input:
50000 50000 11561 23122 14261 28523 24407 48814 17947 35894 14803 29607 19557 39115 12415 24830 9583 19167 18397 36794 439 878 18040 36080 17150 34300 7922 15845 18938 37877 18625 37250 6459 12919 9818 19636 3579 7158 21323 42646 23882 47764 13603 27207 8353 16707 15907 31814 20935 41871 11686 23372...
output:
3 3 2 3 3 3 1 3 3 1 2 3 3 3 3 3 1 3 3 3 3 3 3 2 3 3 3 3 3 3 1 3 2 3 3 4 3 3 3 3 3 3 2 3 4 3 4 3 4 3 3 4 4 3 4 3 3 2 3 3 4 3 4 4 3 3 2 3 1 4 1 1 4 3 2 3 3 3 1 3 3 3 4 4 3 2 3 4 3 2 3 3 3 3 4 3 4 3 3 3 2 3 1 3 3 4 3 4 3 3 2 3 3 1 4 3 3 2 3 3 3 3 3 3 3 2 3 4 3 4 4 3 3 4 3 4 4 3 2 4 3 3 1 2 3 3 2 4 4 3 ...
result:
wrong answer 1st lines differ - expected: '4', found: '3'