Submission #609641

# Submission time Handle Problem Language Result Execution time Memory
609641 2022-07-27T18:44:46 Z gagik_2007 Jail (JOI22_jail) C++17
66 / 100
736 ms 25072 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef ll itn;

#define ff first
#define ss second

string findSum(string str1, string str2)
{
    if (str1.length() > str2.length())
        swap(str1, str2);
    string str = "";
    ll n1 = str1.length(), n2 = str2.length();
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());
    ll carry = 0;
    for (ll i = 0; i < n1; i++)
    {
        ll sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
        str.push_back(sum % 10 + '0');
        carry = sum / 10;
    }
    for (ll i = n1; i < n2; i++)
    {
        ll sum = ((str2[i] - '0') + carry);
        str.push_back(sum % 10 + '0');
        carry = sum / 10;
    }
    if (carry)
        str.push_back(carry + '0');
    reverse(str.begin(), str.end());
    return str;
}
bool isSmaller(string str1, string str2)
{
    ll n1 = str1.length(), n2 = str2.length();
    if (n1 < n2)
        return true;
    if (n2 < n1)
        return false;
    for (ll i = 0; i < n1; i++)
        if (str1[i] < str2[i])
            return true;
        else if (str1[i] > str2[i])
            return false;
    return false;
}
string findDiff(string str1, string str2)
{
    if (isSmaller(str1, str2))
        swap(str1, str2);
    string str = "";
    ll n1 = str1.length(), n2 = str2.length();
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());
    ll carry = 0;
    for (ll i = 0; i < n2; i++) {
        ll sub
            = ((str1[i] - '0') - (str2[i] - '0') - carry);
        if (sub < 0) {
            sub = sub + 10;
            carry = 1;
        }
        else
            carry = 0;
        str.push_back(sub + '0');
    }
    for (ll i = n2; i < n1; i++) {
        ll sub = ((str1[i] - '0') - carry);
        if (sub < 0) {
            sub = sub + 10;
            carry = 1;
        }
        else
            carry = 0;
        str.push_back(sub + '0');
    }
    reverse(str.begin(), str.end());
    return str;
}

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 32768;
const ll N = 100007;
ll n, m, k;
vector<int>g[120007];
ll s[120007];
ll f[120007];
queue<int>q;

struct Point {
    int ind, val, tp;
    Point(int i, int vl, int tip) {
        ind = i;
        val = vl;
        tp = tip;
    }
    bool operator<(Point other)const {
        if (val < other.val)return true;
        else if (val > other.val)return false;
        else return tp < other.tp;
    }
};

vector<Point>d;
int tin[120007];
int tout[120007];
int timer = 0;
int up[120007][20];
int lg;

void dfs(int v, int par) {
    tin[v] = timer++;
    up[v][0] = par;
    for (int i = 0; i < g[v].size(); i++) {
        int to = g[v][i];
        if (to != par) {
            dfs(to, v);
        }
    }
    tout[v] = timer++;
}

void calc() {
    for (int i = 1; i <= lg; i++) {
        for (int v = 1; v <= n; v++) {
            up[v][i] = up[up[v][i - 1]][i - 1];
        }
    }
}

bool pap(int v, int u) {
    return tin[v]<=tin[u] && tout[v]>=tout[u];
}

int lca(int v, int u) {
    if (pap(v, u)) {
        return v;
    }
    if (pap(u, v)) {
        return u;
    }
    for (int i = lg; i >= 0; i--) {
        if (!pap(up[u][i], v)) {
            u = up[u][i];
        }
    }
    //cout << "lca of " << v << " and " << u << " is: " << up[u][0] << endl;
    return up[u][0];
}

bool votitak(int v, int u, int c) {
    return pap(lca(v, u), c) && (pap(c, u) || pap(c, v));
}

vector<int> champa(int v, int u){
    int papa = lca(v,u);
    vector<int>ans;
    while(v!=papa){
        ans.push_back(v);
        v=up[v][0];
    }
    while(u!=papa){
        ans.push_back(u);
        u=up[u][0];
    }
    ans.push_back(papa);
    return ans;
}

vector<int>g2[120007];
int color[120007];
int prc[120007];
int skz[120007];

bool is_cyclic(int v){
    color[v]=1;
    bool ans=false;
    for(int i=0;i<g2[v].size();i++){
        int to=g2[v][i];
        if(color[to]==1)ans=true;
        if(color[to]==0){
            if(is_cyclic(to)){
                ans=true;
            }
        }
    }
    color[v]=2;
    return ans;
}

void sax_jnjxel(){
    timer = 0;
    fill(tin, tin + 120007, 0);
    fill(tout, tout + 120007, 0);
    fill(color,color+120007,0);
    fill(prc,prc+120007,0);
    fill(skz,skz+120007,0);
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        g2[i].clear();
    }
}

void solve(){
    cin >> n;
    lg = ceil(log2(n));
    bool ent1 = true;
    for (int i = 0; i < n - 1; i++) {
        ll x, y;
        cin >> x >> y;
        if (x != i + 1 || y != i + 2) {
            ent1 = false;
        }
        g[x].push_back(y);
        g[y].push_back(x);
    }
    cin >> m;
    for (int i = 1; i <= m; i++) {
        cin >> s[i] >> f[i];
    }
    //ent1 = false;
    if (ent1) {
        for (int i = 1; i <= m; i++) {
            Point x(i, s[i], 1), y(i, f[i], -1);
            d.push_back(x);
            d.push_back(y);
        }
        sort(d.begin(), d.end());
        bool aj = (d[0].tp == 1);
        bool ok = true;
        for (auto x : d) {
            //cout << x.ind << " " << x.tp << "  ";
            if (aj) {
                if (x.tp == 1) {
                    q.push(x.ind);
                }
                else {
                    if (q.empty()) {
                        aj = false;
                        q.push(x.ind);
                    }
                    else {
                        if (x.ind != q.front()) {
                            ok = false;
                            break;
                        }
                        else {
                            q.pop();
                        }
                    }
                }
            }
            else {
                if (x.tp == -1) {
                    q.push(x.ind);
                }
                else {
                    if (q.empty()) {
                        aj = true;
                        q.push(x.ind);
                    }
                    else {
                        if (x.ind != q.front()) {
                            ok = false;
                            break;
                        }
                        else {
                            q.pop();
                        }
                    }
                }
            }
        }
        cout << (ok ? "Yes" : "No") << endl;
        while (!q.empty()) {
            q.pop();
        }
        d.clear();
        for (int i = 1; i <= n; i++) {
            g[i].clear();
        }
    }
    else if(m<=500){
        dfs(1, 1);
        calc();
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                if(j!=i){
                    bool skizb = votitak(f[i],s[i],f[j]);
                    bool verj = votitak(f[i],s[i],s[j]);
                    if(skizb&&verj){
                        //cout<<i<<" "<<j<<endl;
                        cout<<"No"<<endl;
                        sax_jnjxel();
                        return;
                    }
                    if(skizb){
                        g2[i].push_back(j);
                    }
                    else if(verj){
                        g2[j].push_back(i);
                    }
                }
            }
        }
        bool res=false;
        for(int i=1;i<=m;i++){
            if(color[i]==0){
                if(is_cyclic(i)){
                    res=true;
                    break;
                }
            }
        }
        if(!res){
            cout<<"Yes"<<endl;
        }
        else cout<<"No"<<endl;
        sax_jnjxel();
    }
    else{
        dfs(1, 1);
        calc();
        for(int i=1;i<=m;i++){
            prc[s[i]]=i;
            skz[f[i]]=i;
        }
        for(int i=1;i<=m;i++){
            vector<int>c=champa(f[i],s[i]);
            for(auto x:c){
                if(prc[x]!=0&&prc[x]!=i){
                    g2[prc[x]].push_back(i);
                }
                if(skz[x]!=0&&skz[x]!=i){
                    g2[x].push_back(skz[x]);
                }
            }
        }
        bool res=false;
        for(int i=1;i<=m;i++){
            if(color[i]==0){
                if(is_cyclic(i)){
                    res=true;
                    break;
                }
            }
        }
        if(!res){
            cout<<"Yes"<<endl;
        }
        else cout<<"No"<<endl;
        sax_jnjxel();
    }
}

int main() {
    cin >> ttt;
    while (ttt--) {
        solve();
    }
}

/*
4
7
1 2
1 3
2 4
2 5
3 6
3 7
2
4 3
2 7
7
1 2
1 3
2 4
2 5
3 6
3 7
2
4 3
7 2
7
1 2
1 3
2 4
2 5
3 6
3 7
2
4 7
3 2
7
1 2
1 3
2 4
2 5
3 6
3 7
2
4 7
3 4
*/

Compilation message

jail.cpp: In function 'void dfs(int, int)':
jail.cpp:121:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for (int i = 0; i < g[v].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
jail.cpp: In function 'bool is_cyclic(int)':
jail.cpp:185:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |     for(int i=0;i<g2[v].size();i++){
      |                 ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 23 ms 5880 KB Output is correct
5 Correct 49 ms 5860 KB Output is correct
6 Correct 5 ms 5904 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 6 ms 5972 KB Output is correct
9 Correct 62 ms 6100 KB Output is correct
10 Correct 76 ms 9656 KB Output is correct
11 Correct 13 ms 5844 KB Output is correct
12 Correct 56 ms 5972 KB Output is correct
13 Correct 119 ms 12112 KB Output is correct
14 Correct 116 ms 13252 KB Output is correct
15 Correct 120 ms 13108 KB Output is correct
16 Correct 166 ms 15728 KB Output is correct
17 Correct 116 ms 13252 KB Output is correct
18 Correct 157 ms 15732 KB Output is correct
19 Correct 113 ms 13300 KB Output is correct
20 Correct 118 ms 13292 KB Output is correct
21 Correct 129 ms 13288 KB Output is correct
22 Correct 120 ms 13364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 5 ms 5844 KB Output is correct
4 Correct 8 ms 8276 KB Output is correct
5 Correct 9 ms 8276 KB Output is correct
6 Correct 8 ms 8276 KB Output is correct
7 Correct 8 ms 8276 KB Output is correct
8 Correct 8 ms 8276 KB Output is correct
9 Correct 12 ms 8336 KB Output is correct
10 Correct 8 ms 8276 KB Output is correct
11 Correct 7 ms 8236 KB Output is correct
12 Correct 6 ms 8276 KB Output is correct
13 Correct 10 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 5 ms 5844 KB Output is correct
4 Correct 8 ms 8276 KB Output is correct
5 Correct 9 ms 8276 KB Output is correct
6 Correct 8 ms 8276 KB Output is correct
7 Correct 8 ms 8276 KB Output is correct
8 Correct 8 ms 8276 KB Output is correct
9 Correct 12 ms 8336 KB Output is correct
10 Correct 8 ms 8276 KB Output is correct
11 Correct 7 ms 8236 KB Output is correct
12 Correct 6 ms 8276 KB Output is correct
13 Correct 10 ms 8276 KB Output is correct
14 Correct 4 ms 8276 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 8 ms 5972 KB Output is correct
17 Correct 7 ms 8276 KB Output is correct
18 Correct 7 ms 8276 KB Output is correct
19 Correct 8 ms 8280 KB Output is correct
20 Correct 8 ms 8276 KB Output is correct
21 Correct 13 ms 8276 KB Output is correct
22 Correct 7 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 8 ms 8276 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 7 ms 8276 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 5 ms 5844 KB Output is correct
4 Correct 8 ms 8276 KB Output is correct
5 Correct 9 ms 8276 KB Output is correct
6 Correct 8 ms 8276 KB Output is correct
7 Correct 8 ms 8276 KB Output is correct
8 Correct 8 ms 8276 KB Output is correct
9 Correct 12 ms 8336 KB Output is correct
10 Correct 8 ms 8276 KB Output is correct
11 Correct 7 ms 8236 KB Output is correct
12 Correct 6 ms 8276 KB Output is correct
13 Correct 10 ms 8276 KB Output is correct
14 Correct 4 ms 8276 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 8 ms 5972 KB Output is correct
17 Correct 7 ms 8276 KB Output is correct
18 Correct 7 ms 8276 KB Output is correct
19 Correct 8 ms 8280 KB Output is correct
20 Correct 8 ms 8276 KB Output is correct
21 Correct 13 ms 8276 KB Output is correct
22 Correct 7 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 8 ms 8276 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 7 ms 8276 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
29 Correct 6 ms 5972 KB Output is correct
30 Correct 14 ms 8276 KB Output is correct
31 Correct 9 ms 8276 KB Output is correct
32 Correct 13 ms 8276 KB Output is correct
33 Correct 10 ms 8268 KB Output is correct
34 Correct 19 ms 8348 KB Output is correct
35 Correct 14 ms 8276 KB Output is correct
36 Correct 28 ms 8344 KB Output is correct
37 Correct 20 ms 8348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 5 ms 5844 KB Output is correct
4 Correct 8 ms 8276 KB Output is correct
5 Correct 9 ms 8276 KB Output is correct
6 Correct 8 ms 8276 KB Output is correct
7 Correct 8 ms 8276 KB Output is correct
8 Correct 8 ms 8276 KB Output is correct
9 Correct 12 ms 8336 KB Output is correct
10 Correct 8 ms 8276 KB Output is correct
11 Correct 7 ms 8236 KB Output is correct
12 Correct 6 ms 8276 KB Output is correct
13 Correct 10 ms 8276 KB Output is correct
14 Correct 4 ms 8276 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 8 ms 5972 KB Output is correct
17 Correct 7 ms 8276 KB Output is correct
18 Correct 7 ms 8276 KB Output is correct
19 Correct 8 ms 8280 KB Output is correct
20 Correct 8 ms 8276 KB Output is correct
21 Correct 13 ms 8276 KB Output is correct
22 Correct 7 ms 8276 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 8 ms 8276 KB Output is correct
26 Correct 5 ms 8276 KB Output is correct
27 Correct 7 ms 8276 KB Output is correct
28 Correct 5 ms 8276 KB Output is correct
29 Correct 6 ms 5972 KB Output is correct
30 Correct 14 ms 8276 KB Output is correct
31 Correct 9 ms 8276 KB Output is correct
32 Correct 13 ms 8276 KB Output is correct
33 Correct 10 ms 8268 KB Output is correct
34 Correct 19 ms 8348 KB Output is correct
35 Correct 14 ms 8276 KB Output is correct
36 Correct 28 ms 8344 KB Output is correct
37 Correct 20 ms 8348 KB Output is correct
38 Correct 67 ms 6100 KB Output is correct
39 Correct 89 ms 9704 KB Output is correct
40 Correct 421 ms 9140 KB Output is correct
41 Correct 736 ms 9040 KB Output is correct
42 Correct 494 ms 9364 KB Output is correct
43 Correct 74 ms 9116 KB Output is correct
44 Correct 363 ms 8544 KB Output is correct
45 Correct 191 ms 21368 KB Output is correct
46 Correct 177 ms 21488 KB Output is correct
47 Correct 175 ms 25068 KB Output is correct
48 Correct 181 ms 25072 KB Output is correct
49 Correct 121 ms 21568 KB Output is correct
50 Correct 189 ms 21508 KB Output is correct
51 Correct 114 ms 22628 KB Output is correct
52 Correct 122 ms 22672 KB Output is correct
53 Correct 572 ms 9272 KB Output is correct
54 Correct 216 ms 21312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5844 KB Output is correct
2 Correct 5 ms 8276 KB Output is correct
3 Correct 4 ms 5844 KB Output is correct
4 Correct 3 ms 5844 KB Output is correct
5 Correct 14 ms 5844 KB Output is correct
6 Correct 7 ms 8276 KB Output is correct
7 Correct 9 ms 8276 KB Output is correct
8 Correct 7 ms 8276 KB Output is correct
9 Correct 6 ms 8276 KB Output is correct
10 Correct 6 ms 8276 KB Output is correct
11 Correct 6 ms 8276 KB Output is correct
12 Correct 26 ms 8276 KB Output is correct
13 Correct 140 ms 8312 KB Output is correct
14 Correct 207 ms 8304 KB Output is correct
15 Correct 183 ms 8324 KB Output is correct
16 Incorrect 183 ms 21928 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 23 ms 5880 KB Output is correct
5 Correct 49 ms 5860 KB Output is correct
6 Correct 5 ms 5904 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 6 ms 5972 KB Output is correct
9 Correct 62 ms 6100 KB Output is correct
10 Correct 76 ms 9656 KB Output is correct
11 Correct 13 ms 5844 KB Output is correct
12 Correct 56 ms 5972 KB Output is correct
13 Correct 119 ms 12112 KB Output is correct
14 Correct 116 ms 13252 KB Output is correct
15 Correct 120 ms 13108 KB Output is correct
16 Correct 166 ms 15728 KB Output is correct
17 Correct 116 ms 13252 KB Output is correct
18 Correct 157 ms 15732 KB Output is correct
19 Correct 113 ms 13300 KB Output is correct
20 Correct 118 ms 13292 KB Output is correct
21 Correct 129 ms 13288 KB Output is correct
22 Correct 120 ms 13364 KB Output is correct
23 Correct 3 ms 5844 KB Output is correct
24 Correct 4 ms 5844 KB Output is correct
25 Correct 5 ms 5844 KB Output is correct
26 Correct 8 ms 8276 KB Output is correct
27 Correct 9 ms 8276 KB Output is correct
28 Correct 8 ms 8276 KB Output is correct
29 Correct 8 ms 8276 KB Output is correct
30 Correct 8 ms 8276 KB Output is correct
31 Correct 12 ms 8336 KB Output is correct
32 Correct 8 ms 8276 KB Output is correct
33 Correct 7 ms 8236 KB Output is correct
34 Correct 6 ms 8276 KB Output is correct
35 Correct 10 ms 8276 KB Output is correct
36 Correct 4 ms 8276 KB Output is correct
37 Correct 3 ms 5844 KB Output is correct
38 Correct 8 ms 5972 KB Output is correct
39 Correct 7 ms 8276 KB Output is correct
40 Correct 7 ms 8276 KB Output is correct
41 Correct 8 ms 8280 KB Output is correct
42 Correct 8 ms 8276 KB Output is correct
43 Correct 13 ms 8276 KB Output is correct
44 Correct 7 ms 8276 KB Output is correct
45 Correct 5 ms 8276 KB Output is correct
46 Correct 6 ms 8276 KB Output is correct
47 Correct 8 ms 8276 KB Output is correct
48 Correct 5 ms 8276 KB Output is correct
49 Correct 7 ms 8276 KB Output is correct
50 Correct 5 ms 8276 KB Output is correct
51 Correct 6 ms 5972 KB Output is correct
52 Correct 14 ms 8276 KB Output is correct
53 Correct 9 ms 8276 KB Output is correct
54 Correct 13 ms 8276 KB Output is correct
55 Correct 10 ms 8268 KB Output is correct
56 Correct 19 ms 8348 KB Output is correct
57 Correct 14 ms 8276 KB Output is correct
58 Correct 28 ms 8344 KB Output is correct
59 Correct 20 ms 8348 KB Output is correct
60 Correct 67 ms 6100 KB Output is correct
61 Correct 89 ms 9704 KB Output is correct
62 Correct 421 ms 9140 KB Output is correct
63 Correct 736 ms 9040 KB Output is correct
64 Correct 494 ms 9364 KB Output is correct
65 Correct 74 ms 9116 KB Output is correct
66 Correct 363 ms 8544 KB Output is correct
67 Correct 191 ms 21368 KB Output is correct
68 Correct 177 ms 21488 KB Output is correct
69 Correct 175 ms 25068 KB Output is correct
70 Correct 181 ms 25072 KB Output is correct
71 Correct 121 ms 21568 KB Output is correct
72 Correct 189 ms 21508 KB Output is correct
73 Correct 114 ms 22628 KB Output is correct
74 Correct 122 ms 22672 KB Output is correct
75 Correct 572 ms 9272 KB Output is correct
76 Correct 216 ms 21312 KB Output is correct
77 Correct 4 ms 5844 KB Output is correct
78 Correct 5 ms 8276 KB Output is correct
79 Correct 4 ms 5844 KB Output is correct
80 Correct 3 ms 5844 KB Output is correct
81 Correct 14 ms 5844 KB Output is correct
82 Correct 7 ms 8276 KB Output is correct
83 Correct 9 ms 8276 KB Output is correct
84 Correct 7 ms 8276 KB Output is correct
85 Correct 6 ms 8276 KB Output is correct
86 Correct 6 ms 8276 KB Output is correct
87 Correct 6 ms 8276 KB Output is correct
88 Correct 26 ms 8276 KB Output is correct
89 Correct 140 ms 8312 KB Output is correct
90 Correct 207 ms 8304 KB Output is correct
91 Correct 183 ms 8324 KB Output is correct
92 Incorrect 183 ms 21928 KB Output isn't correct
93 Halted 0 ms 0 KB -