Submission #336146

# Submission time Handle Problem Language Result Execution time Memory
336146 2020-12-14T22:03:26 Z gmyu Railway (BOI17_railway) C++14
100 / 100
254 ms 44140 KB
/*
ID: USACO_template
LANG: C++
PROG: USACO
*/
#include <iostream>  //cin , cout
#include <fstream>   //fin, fout
#include <stdio.h>   // scanf , pringf
#include <cstdio>
#include <algorithm> // sort , stuff
#include <stack>     // stacks
#include <queue>     // queues
#include <map>
#include <string>

using namespace std;

typedef pair<int, int>          pii;
typedef vector<int>             vi;     /// adjlist without weight
typedef vector<pii>             vii;    /// adjlist with weight
typedef vector<pair<int,pii>>   vpip;   /// edge with weight
typedef long long               ll;

#define mp  make_pair
#define fst first
#define snd second
#define pb  push_back
#define sz(x) (int)(x).size()
#define trav(u, adj_v) for (auto& u: adj_v)

const int MOD = 1e9+7;  // 998244353;
const int MX  = 2e5+5;   //
const ll  INF = 1e18;    //

#define MAXV 100007
#define MAXE 100007

const int xdir[4] = {1,0,-1,0}, ydir[4] = {0,1,0,-1}; /// 4 directions
struct NODE {
    int x, y;
    int val;
    int visited;
    bool operator< (NODE b) const { return (x == b.x) ? (y < b.y) : (x < b.x); }
};
struct EDGE {
    int from, to;
    ll weight;
    bool operator<(EDGE other) const { return weight < other.weight; }
};

/// code from USACO examples
void setIO(string name) {
    ios_base::sync_with_stdio(0); cin.tie(0);
    freopen((name+".in").c_str(),"r",stdin);
    freopen((name+".out").c_str(),"w",stdout);
}
bool debug = false, submit = true;

int N, M, K;
vi adjlist[MAXV];
map<int, int> pathmap[MAXV];
int dp[MAXV];

/// Euler ID for the tree
int p[MAXV], level[MAXV];
pii eulerRange[MAXV];
int eulerID = 0;

void buildEulerTree(int u, int lv) {

    eulerID++;
    eulerRange[u].first = eulerID;

    for(int i=0; i<adjlist[u].size(); i++) {
        int v = adjlist[u][i];
        if(v != p[u]) {
            p[v] = u;
            level[v] = lv + 1;
            buildEulerTree(v, lv+1);
        }
    }

    eulerRange[u].second = eulerID;
}
void initEuler() {
    /// root = 1
    adjlist[1].push_back(0);
    p[1] = 0; level[1] = 1;    //p[0] = 0; level[0] = 0;

    eulerID = 0;
    buildEulerTree(1, 1);
}
bool cmdFunc(const int& v1, const int& v2) {
    return eulerRange[v1].fst < eulerRange[v2].first;
}

/// tree info for calculate LCA
#define MAXLCALOG 20
int depth[MAXV], anc[MAXV][MAXLCALOG];

void prepLCA() {
    for(int i = 1; i <= N; i++) { anc[i][0] = p[i]; depth[i] = level[i] - 1;}

    for(int j = 1; j < MAXLCALOG; j++)
        for(int i = 1; i <= N; i++)
            anc[i][j] = anc[anc[i][j-1]][j-1];
}
int upD(int curr, int dist) {       /// go up by dist
    if(dist == 0) return curr;
    for(int k = MAXLCALOG - 1; k>=0; k--) {
        while(dist >= (1<<k) ) {
            curr = anc[curr][k];
            dist  -= (1<<k);
        }
        if(dist == 0) return curr;
    }
    return curr;
}
int getP(int curr, int wantedDepth) {   /// reach wanted depth
    for(int k = MAXLCALOG - 1; depth[curr] != wantedDepth; k--) {
        while(depth[curr] - (1<<k) >= wantedDepth) {
            curr = anc[curr][k];
        }
    }
    return curr;
}
int getLCA(int a, int b) {
    if(depth[a] > depth[b]) {
        return getLCA(b, a);
    }
    if(depth[a] < depth[b]) {
        b = getP(b, depth[a]);
    }
    for(int k = MAXLCALOG - 1; k > 0; k--) {
        while(anc[a][k] != anc[b][k]) {
            a = anc[a][k];
            b = anc[b][k];
        }
    }
    while(a != b) {
        a = p[a];
        b = p[b];
    }
    return a;
}

vi ans;
int dfsDP(int v, int pv) {
    if(sz(adjlist[v])==1 && v != 1) { // leaf
        if(dp[v] >= 2*K) ans.pb(pathmap[v][pv]);
        return dp[v];
    }
    for(int x : adjlist[v]) {
        if(x == pv) continue;
        dp[v] += dfsDP(x, v);
    }
    if(dp[v] >= 2*K && v != 1) ans.pb(pathmap[v][pv]);
    return dp[v];
}

int main() {
    debug = true; submit = false;
    if(submit) setIO("testName");

    int i, j, k;

    cin >> N >> M >> K;
    for(i=1; i<=N-1; i++) {
        int a, b; cin >> a >> b;
        adjlist[a].pb(b); adjlist[b].pb(a);
        pathmap[a][b] = i; pathmap[b][a] = i;
    }
    initEuler();
    prepLCA();

    vi cmd;
    for(i=1; i<=M; i++) {
        cmd.clear();
        int a; cin >> a;
        for(j=0; j<a; j++) { int b; cin >> b; cmd.pb(b); }
        sort(cmd.begin(), cmd.end(), cmdFunc);
        int b = cmd[0]; cmd.pb(b);
        for(j=0; j < sz(cmd) - 1; j++) {
            a = cmd[j]; b = cmd[j+1];
            int c = getLCA(a, b);
            dp[a]++; dp[c]--;
            dp[b]++; dp[c]--;
        }
    }

    dfsDP(1, 0);
    sort(ans.begin(), ans.end());
    cout << sz(ans) << endl;
    for(int x : ans) cout << x << " ";
    cout << endl;

}

Compilation message

railway.cpp: In function 'void buildEulerTree(int, int)':
railway.cpp:74:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for(int i=0; i<adjlist[u].size(); i++) {
      |                  ~^~~~~~~~~~~~~~~~~~
railway.cpp: In function 'int main()':
railway.cpp:165:15: warning: unused variable 'k' [-Wunused-variable]
  165 |     int i, j, k;
      |               ^
railway.cpp: In function 'void setIO(std::string)':
railway.cpp:54:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   54 |     freopen((name+".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:55:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   55 |     freopen((name+".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7532 KB Output is correct
2 Correct 17 ms 9708 KB Output is correct
3 Correct 18 ms 9708 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 21 ms 10348 KB Output is correct
7 Correct 18 ms 9836 KB Output is correct
8 Correct 16 ms 9964 KB Output is correct
9 Correct 16 ms 9836 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 6 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7532 KB Output is correct
2 Correct 17 ms 9708 KB Output is correct
3 Correct 18 ms 9708 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 21 ms 10348 KB Output is correct
7 Correct 18 ms 9836 KB Output is correct
8 Correct 16 ms 9964 KB Output is correct
9 Correct 16 ms 9836 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 6 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7552 KB Output is correct
15 Correct 52 ms 10220 KB Output is correct
16 Correct 51 ms 10220 KB Output is correct
17 Correct 52 ms 10220 KB Output is correct
18 Correct 24 ms 10348 KB Output is correct
19 Correct 18 ms 9836 KB Output is correct
20 Correct 50 ms 10348 KB Output is correct
21 Correct 51 ms 10220 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 18 ms 9836 KB Output is correct
24 Correct 18 ms 9708 KB Output is correct
25 Correct 5 ms 7404 KB Output is correct
26 Correct 5 ms 7404 KB Output is correct
27 Correct 18 ms 10348 KB Output is correct
28 Correct 18 ms 9856 KB Output is correct
29 Correct 16 ms 9836 KB Output is correct
30 Correct 17 ms 9836 KB Output is correct
31 Correct 5 ms 7404 KB Output is correct
32 Correct 5 ms 7424 KB Output is correct
33 Correct 5 ms 7404 KB Output is correct
34 Correct 5 ms 7404 KB Output is correct
35 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 43916 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 227 ms 43372 KB Output is correct
4 Correct 221 ms 42860 KB Output is correct
5 Correct 252 ms 43884 KB Output is correct
6 Correct 234 ms 44140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 37356 KB Output is correct
2 Correct 241 ms 32620 KB Output is correct
3 Correct 235 ms 32108 KB Output is correct
4 Correct 237 ms 31980 KB Output is correct
5 Correct 238 ms 32108 KB Output is correct
6 Correct 206 ms 37664 KB Output is correct
7 Correct 209 ms 37440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 37356 KB Output is correct
2 Correct 241 ms 32620 KB Output is correct
3 Correct 235 ms 32108 KB Output is correct
4 Correct 237 ms 31980 KB Output is correct
5 Correct 238 ms 32108 KB Output is correct
6 Correct 206 ms 37664 KB Output is correct
7 Correct 209 ms 37440 KB Output is correct
8 Correct 221 ms 37484 KB Output is correct
9 Correct 219 ms 37612 KB Output is correct
10 Correct 207 ms 43880 KB Output is correct
11 Correct 208 ms 44008 KB Output is correct
12 Correct 187 ms 31852 KB Output is correct
13 Correct 197 ms 31852 KB Output is correct
14 Correct 231 ms 31980 KB Output is correct
15 Correct 231 ms 31852 KB Output is correct
16 Correct 238 ms 32108 KB Output is correct
17 Correct 238 ms 31980 KB Output is correct
18 Correct 241 ms 32324 KB Output is correct
19 Correct 226 ms 32492 KB Output is correct
20 Correct 209 ms 37996 KB Output is correct
21 Correct 208 ms 37868 KB Output is correct
22 Correct 208 ms 37996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7532 KB Output is correct
2 Correct 17 ms 9708 KB Output is correct
3 Correct 18 ms 9708 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 21 ms 10348 KB Output is correct
7 Correct 18 ms 9836 KB Output is correct
8 Correct 16 ms 9964 KB Output is correct
9 Correct 16 ms 9836 KB Output is correct
10 Correct 5 ms 7404 KB Output is correct
11 Correct 6 ms 7404 KB Output is correct
12 Correct 6 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7552 KB Output is correct
15 Correct 52 ms 10220 KB Output is correct
16 Correct 51 ms 10220 KB Output is correct
17 Correct 52 ms 10220 KB Output is correct
18 Correct 24 ms 10348 KB Output is correct
19 Correct 18 ms 9836 KB Output is correct
20 Correct 50 ms 10348 KB Output is correct
21 Correct 51 ms 10220 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 18 ms 9836 KB Output is correct
24 Correct 18 ms 9708 KB Output is correct
25 Correct 5 ms 7404 KB Output is correct
26 Correct 5 ms 7404 KB Output is correct
27 Correct 18 ms 10348 KB Output is correct
28 Correct 18 ms 9856 KB Output is correct
29 Correct 16 ms 9836 KB Output is correct
30 Correct 17 ms 9836 KB Output is correct
31 Correct 5 ms 7404 KB Output is correct
32 Correct 5 ms 7424 KB Output is correct
33 Correct 5 ms 7404 KB Output is correct
34 Correct 5 ms 7404 KB Output is correct
35 Correct 5 ms 7404 KB Output is correct
36 Correct 231 ms 43916 KB Output is correct
37 Correct 5 ms 7404 KB Output is correct
38 Correct 227 ms 43372 KB Output is correct
39 Correct 221 ms 42860 KB Output is correct
40 Correct 252 ms 43884 KB Output is correct
41 Correct 234 ms 44140 KB Output is correct
42 Correct 203 ms 37356 KB Output is correct
43 Correct 241 ms 32620 KB Output is correct
44 Correct 235 ms 32108 KB Output is correct
45 Correct 237 ms 31980 KB Output is correct
46 Correct 238 ms 32108 KB Output is correct
47 Correct 206 ms 37664 KB Output is correct
48 Correct 209 ms 37440 KB Output is correct
49 Correct 221 ms 37484 KB Output is correct
50 Correct 219 ms 37612 KB Output is correct
51 Correct 207 ms 43880 KB Output is correct
52 Correct 208 ms 44008 KB Output is correct
53 Correct 187 ms 31852 KB Output is correct
54 Correct 197 ms 31852 KB Output is correct
55 Correct 231 ms 31980 KB Output is correct
56 Correct 231 ms 31852 KB Output is correct
57 Correct 238 ms 32108 KB Output is correct
58 Correct 238 ms 31980 KB Output is correct
59 Correct 241 ms 32324 KB Output is correct
60 Correct 226 ms 32492 KB Output is correct
61 Correct 209 ms 37996 KB Output is correct
62 Correct 208 ms 37868 KB Output is correct
63 Correct 208 ms 37996 KB Output is correct
64 Correct 218 ms 37740 KB Output is correct
65 Correct 245 ms 31980 KB Output is correct
66 Correct 254 ms 32128 KB Output is correct
67 Correct 238 ms 31852 KB Output is correct
68 Correct 179 ms 31724 KB Output is correct
69 Correct 179 ms 31724 KB Output is correct
70 Correct 223 ms 37996 KB Output is correct
71 Correct 221 ms 37356 KB Output is correct
72 Correct 5 ms 7404 KB Output is correct
73 Correct 18 ms 9836 KB Output is correct
74 Correct 20 ms 9708 KB Output is correct
75 Correct 5 ms 7404 KB Output is correct
76 Correct 5 ms 7404 KB Output is correct
77 Correct 18 ms 10348 KB Output is correct
78 Correct 18 ms 9836 KB Output is correct
79 Correct 15 ms 9836 KB Output is correct
80 Correct 17 ms 9836 KB Output is correct
81 Correct 5 ms 7404 KB Output is correct
82 Correct 6 ms 7404 KB Output is correct
83 Correct 5 ms 7404 KB Output is correct
84 Correct 5 ms 7404 KB Output is correct
85 Correct 5 ms 7404 KB Output is correct
86 Correct 50 ms 10220 KB Output is correct
87 Correct 52 ms 10240 KB Output is correct
88 Correct 51 ms 10220 KB Output is correct
89 Correct 18 ms 10348 KB Output is correct
90 Correct 18 ms 9836 KB Output is correct
91 Correct 50 ms 10348 KB Output is correct
92 Correct 52 ms 10348 KB Output is correct
93 Correct 5 ms 7404 KB Output is correct
94 Correct 229 ms 43880 KB Output is correct
95 Correct 225 ms 43500 KB Output is correct
96 Correct 213 ms 42860 KB Output is correct
97 Correct 232 ms 43776 KB Output is correct
98 Correct 227 ms 44140 KB Output is correct
99 Correct 237 ms 31980 KB Output is correct
100 Correct 241 ms 31980 KB Output is correct
101 Correct 228 ms 31980 KB Output is correct
102 Correct 230 ms 32492 KB Output is correct
103 Correct 202 ms 37484 KB Output is correct
104 Correct 208 ms 37868 KB Output is correct
105 Correct 205 ms 37484 KB Output is correct
106 Correct 216 ms 37740 KB Output is correct
107 Correct 216 ms 37612 KB Output is correct
108 Correct 207 ms 43880 KB Output is correct
109 Correct 213 ms 43752 KB Output is correct
110 Correct 186 ms 31852 KB Output is correct
111 Correct 188 ms 31852 KB Output is correct
112 Correct 226 ms 31852 KB Output is correct
113 Correct 228 ms 31852 KB Output is correct