Submission #962571

# Submission time Handle Problem Language Result Execution time Memory
962571 2024-04-13T20:17:24 Z amine_aroua Tourism (JOI23_tourism) C++17
5 / 100
5000 ms 14792 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>

const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;

ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)

// HERE IS THE SOLUTION
int n , m , q;
vector<vi> adj;
vi c;
vi depth;
vi par;
void dfs(int x , int p)
{
    for(auto u : adj[x])
    {
        if(u == p)
            continue;
        depth[u] = depth[x] + 1;
        par[u] = x;
        dfs(u , x);
    }
}
void get_together(int u , int v , set<int> &vis)
{
    if(u == v)
    {
        vis.insert(u);
        return ;
    }
    if(depth[u] < depth[v])
        swap(u , v);
    int x = depth[u] - depth[v];
    int ans = 0;
    vis.insert(u);
    while (x--)
    {
        ans++;
        u = par[u];
        vis.insert(u);
    }
    if(u == v)
    {
        return;
    }
    vis.insert(v);
    while(u != v)
    {
        ans+=2;
        u = par[u];
        vis.insert(u);
        v = par[v];
        vis.insert(v);
    }
    return ;
}
signed main()
{
    boost;
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    c = vi (m);
    adj.assign(n , {});
    depth.assign(n , 0);
    par.assign(n , 0);
    fore(i , n - 1)
    {
        int u , v;
        cin>>u>>v;
        u-- , v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    fore(i , m)
    {
        cin>>c[i];
        c[i]--;
    }
    dfs(0 , -1);
    while(q--)
    {
        int l , r;
        cin>>l>>r;
        l-- , r--;
        set<int> vis;
        int i = l;
        vis.insert(c[i]);
        forr(j , i + 1 , r)
        {
            get_together(c[j - 1] , c[j] , vis);
        }
        cout<<sz(vis)<<nl;
    }
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 6 ms 348 KB Output is correct
5 Correct 5 ms 348 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 12 ms 348 KB Output is correct
10 Correct 12 ms 488 KB Output is correct
11 Correct 11 ms 348 KB Output is correct
12 Correct 30 ms 348 KB Output is correct
13 Correct 27 ms 600 KB Output is correct
14 Correct 29 ms 348 KB Output is correct
15 Correct 55 ms 348 KB Output is correct
16 Correct 46 ms 592 KB Output is correct
17 Correct 50 ms 472 KB Output is correct
18 Correct 39 ms 348 KB Output is correct
19 Correct 40 ms 492 KB Output is correct
20 Correct 53 ms 472 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 4 ms 348 KB Output is correct
23 Correct 4 ms 488 KB Output is correct
24 Correct 4 ms 604 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 4 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 6 ms 348 KB Output is correct
5 Correct 5 ms 348 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 12 ms 348 KB Output is correct
10 Correct 12 ms 488 KB Output is correct
11 Correct 11 ms 348 KB Output is correct
12 Correct 30 ms 348 KB Output is correct
13 Correct 27 ms 600 KB Output is correct
14 Correct 29 ms 348 KB Output is correct
15 Correct 55 ms 348 KB Output is correct
16 Correct 46 ms 592 KB Output is correct
17 Correct 50 ms 472 KB Output is correct
18 Correct 39 ms 348 KB Output is correct
19 Correct 40 ms 492 KB Output is correct
20 Correct 53 ms 472 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 4 ms 348 KB Output is correct
23 Correct 4 ms 488 KB Output is correct
24 Correct 4 ms 604 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 4 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 354 ms 620 KB Output is correct
31 Correct 510 ms 604 KB Output is correct
32 Correct 804 ms 600 KB Output is correct
33 Correct 819 ms 684 KB Output is correct
34 Correct 749 ms 684 KB Output is correct
35 Correct 2216 ms 680 KB Output is correct
36 Correct 2356 ms 604 KB Output is correct
37 Correct 2433 ms 672 KB Output is correct
38 Execution timed out 5005 ms 600 KB Time limit exceeded
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 348 KB Output is correct
4 Execution timed out 5053 ms 11460 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 70 ms 5980 KB Output is correct
3 Correct 125 ms 6744 KB Output is correct
4 Correct 89 ms 7364 KB Output is correct
5 Correct 242 ms 14136 KB Output is correct
6 Correct 246 ms 12884 KB Output is correct
7 Correct 224 ms 12004 KB Output is correct
8 Correct 187 ms 11180 KB Output is correct
9 Correct 166 ms 10832 KB Output is correct
10 Correct 156 ms 10976 KB Output is correct
11 Correct 137 ms 10688 KB Output is correct
12 Correct 145 ms 10676 KB Output is correct
13 Correct 135 ms 10760 KB Output is correct
14 Correct 133 ms 10836 KB Output is correct
15 Correct 54 ms 11948 KB Output is correct
16 Correct 179 ms 13392 KB Output is correct
17 Correct 179 ms 13684 KB Output is correct
18 Correct 176 ms 13236 KB Output is correct
19 Execution timed out 5057 ms 14792 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 5 ms 480 KB Output is correct
4 Execution timed out 5055 ms 9044 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 6 ms 348 KB Output is correct
5 Correct 5 ms 348 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 12 ms 348 KB Output is correct
10 Correct 12 ms 488 KB Output is correct
11 Correct 11 ms 348 KB Output is correct
12 Correct 30 ms 348 KB Output is correct
13 Correct 27 ms 600 KB Output is correct
14 Correct 29 ms 348 KB Output is correct
15 Correct 55 ms 348 KB Output is correct
16 Correct 46 ms 592 KB Output is correct
17 Correct 50 ms 472 KB Output is correct
18 Correct 39 ms 348 KB Output is correct
19 Correct 40 ms 492 KB Output is correct
20 Correct 53 ms 472 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 4 ms 348 KB Output is correct
23 Correct 4 ms 488 KB Output is correct
24 Correct 4 ms 604 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 4 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 354 ms 620 KB Output is correct
31 Correct 510 ms 604 KB Output is correct
32 Correct 804 ms 600 KB Output is correct
33 Correct 819 ms 684 KB Output is correct
34 Correct 749 ms 684 KB Output is correct
35 Correct 2216 ms 680 KB Output is correct
36 Correct 2356 ms 604 KB Output is correct
37 Correct 2433 ms 672 KB Output is correct
38 Execution timed out 5005 ms 600 KB Time limit exceeded
39 Halted 0 ms 0 KB -