QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#351539#6559. A Tree and Two EdgesjanYWA 67ms9200kbC++206.4kb2024-03-12 01:20:022024-03-12 01:20:03

Judging History

你现在查看的是最新测评结果

  • [2024-03-12 01:20:03]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:9200kb
  • [2024-03-12 01:20:02]
  • 提交

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 block_path(int loc, int p, int targ){
    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){
        block_path(xtra[0].se, xtra[0].se, xtra[1].fi);
        blocked[xtra[0].fi] = 1;
        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];
    blocked[the_edge.se] = 1;
    fill_subtree(the_edge.se, the_edge.se, 1, 0);
    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: 3680kb

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: 1ms
memory: 3848kb

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: 0
Accepted
time: 58ms
memory: 9200kb

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:

4
3
3
4
4
3
1
3
4
1
3
4
3
4
3
3
1
3
3
3
4
4
4
3
3
3
4
3
3
3
1
3
3
3
3
4
4
4
4
3
4
3
3
3
4
3
4
4
4
4
3
4
4
4
4
3
3
3
4
4
4
4
4
4
3
4
3
4
1
4
1
1
4
3
3
4
3
3
1
4
3
3
4
4
3
3
4
4
4
3
4
3
4
4
4
4
4
3
4
3
3
3
1
3
4
4
3
4
3
4
3
3
4
1
4
3
3
3
4
4
4
4
3
3
3
3
3
4
4
4
4
3
3
4
3
4
4
3
3
4
4
4
1
3
3
3
3
4
4
3
...

result:

ok 50000 lines

Test #4:

score: 0
Accepted
time: 61ms
memory: 9192kb

input:

50000 50000
1730 3460
17535 35071
14108 28216
20630 41260
2091 4182
8112 16225
21373 42746
6685 13371
21142 42284
12168 24337
22564 45128
16103 32207
9254 18508
21369 42739
1955 3911
13696 27392
3929 7858
1777 3555
23382 46764
830 1660
17722 35444
11495 22991
10184 20369
13697 27395
24728 49456
4037...

output:

1
1
3
3
3
4
4
4
4
4
3
3
3
4
1
3
4
3
3
1
3
3
4
3
1
4
3
3
4
3
3
4
4
4
1
1
4
1
3
4
3
1
4
4
3
3
3
4
1
4
4
1
3
1
3
3
3
1
1
3
3
3
3
3
4
3
4
4
3
3
4
4
4
3
3
4
4
4
3
4
3
3
3
3
3
3
3
3
4
4
1
4
3
4
1
4
4
4
4
3
4
1
4
4
3
4
3
4
3
4
3
1
4
3
1
1
3
3
4
4
1
3
3
3
4
3
3
4
4
4
4
4
3
3
4
3
4
1
1
3
4
4
3
4
4
3
3
4
4
3
...

result:

ok 50000 lines

Test #5:

score: 0
Accepted
time: 53ms
memory: 9200kb

input:

50000 50000
21879 43758
12510 25020
2593 5187
16048 32096
9697 19394
12606 25212
3860 7720
8231 16462
23983 47966
10852 21705
6919 13839
1385 2770
4040 8080
14298 28596
22248 44496
4245 8490
14486 28972
11445 22891
21557 43114
20946 41892
23374 46749
78 157
4617 9234
8198 16396
12228 24456
16125 322...

output:

4
2
2
2
4
2
1
2
2
2
4
2
1
2
4
2
2
4
4
4
2
1
4
2
2
4
2
4
2
2
1
4
2
2
1
1
4
4
2
2
2
4
2
4
2
4
4
4
2
2
4
2
2
4
1
4
1
4
4
2
4
4
2
2
2
2
4
2
2
1
2
1
4
2
4
4
4
4
2
2
2
4
4
1
2
1
2
4
4
4
2
2
2
2
4
4
4
4
4
2
4
2
4
4
2
4
4
4
4
4
2
4
2
2
4
4
4
1
2
2
2
4
1
2
4
1
2
2
4
2
4
4
2
4
2
2
4
1
4
1
2
4
2
2
4
4
4
4
4
4
...

result:

ok 50000 lines

Test #6:

score: 0
Accepted
time: 67ms
memory: 9160kb

input:

50000 50000
1451 2795
8910 29108
638 5810
24117 38535
2769 44109
7603 8789
14090 14819
5315 11076
22885 25853
26110 39470
1513 20322
13635 44414
1284 5229
5998 19700
1872 45691
5872 37168
4991 6456
34921 41632
16532 30269
3118 4987
2732 20486
26292 44061
2054 41607
20367 21071
33204 36717
35801 4725...

output:

2
2
4
4
4
4
4
2
4
4
4
4
4
4
4
2
1
4
4
1
4
4
1
4
4
2
4
2
4
2
1
4
4
4
4
1
1
1
4
2
4
1
4
2
4
1
1
2
2
2
4
4
1
1
1
4
1
1
4
2
4
4
1
4
2
2
4
4
4
2
1
4
4
1
1
4
4
1
2
1
2
1
4
2
1
2
4
2
4
4
4
4
4
1
2
2
1
1
1
4
2
1
4
4
2
4
2
4
4
1
1
4
4
2
2
1
2
1
1
4
4
4
4
4
4
1
1
1
4
2
4
1
2
2
1
4
4
4
4
4
4
1
4
4
2
1
2
2
2
1
...

result:

ok 50000 lines

Test #7:

score: -100
Wrong Answer
time: 67ms
memory: 9200kb

input:

50000 50000
1106 3307
13115 16051
30404 45806
2295 20076
3525 6384
9118 24628
3288 26835
17933 47506
26180 48256
23161 45529
10483 15545
5252 35302
10105 16247
14301 26402
104 216
562 29098
1517 16503
1494 5468
8057 47252
5582 15425
8766 41483
10952 31098
20891 49612
13088 18868
18880 28314
8650 208...

output:

1
3
4
2
4
4
2
4
3
4
4
4
2
2
2
2
4
2
4
4
4
2
4
1
2
1
4
4
1
2
4
2
1
4
4
4
4
4
2
4
4
4
4
1
2
1
2
1
4
4
2
3
4
4
1
4
4
4
2
2
2
1
4
2
4
2
4
4
4
2
2
4
2
2
1
4
4
2
4
4
4
2
1
4
1
4
4
4
4
4
4
1
2
2
2
4
1
2
1
4
2
4
2
4
4
4
1
4
2
4
4
2
4
2
4
2
4
4
1
4
4
2
1
4
4
4
2
4
2
2
2
2
2
4
4
2
4
4
2
2
4
1
2
1
4
4
2
3
3
4
...

result:

wrong answer 2nd lines differ - expected: '2', found: '3'