제출 #1348713

#제출 시각아이디문제언어결과실행 시간메모리
1348713nguyenkhangninh99Unique Cities (JOI19_ho_t5)C++20
컴파일 에러
0 ms0 KiB
include <bits/stdc++.h>
using namespace std;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;

    vector<vector<int>> adj(n + 1);
    for(int i = 1; i <= n - 1; i++){
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    vector<int> typ(n + 1);
    for (int i = 1; i <= n; ++i) cin >> typ[i];

    vector<int> dist(n + 1), len(n + 1), res(n + 1, 0);

    function<void(int, int, int)> dfs = [&](int u, int p, int d){
        dist[u] = d;
        len[u] = 0;
        for(int v: adj[u]){
            if(v == p) continue;
            dfs(v, u, d + 1);
            len[u] = max(len[u], len[v] + 1);
        }
    };

    vector<int> cnt(m + 1);
    int resact = 0;
    vector<int> pila; 

    auto add = [&](int u) {
        if (++cnt[typ[u]] == 1) resact++;
    };
    auto del = [&](int u) {
        if (--cnt[typ[u]] == 0) resact--;
    };

    function<void(int, int)> ndfs = [&](int u, int p){
        vector<pair<int, int>> radj;
        for(int v : adj[u]){
            if(v != p) radj.push_back({len[v] + 1, v});
        }
        sort(radj.begin(), radj.end(), greater<pair<int, int>>());
        int mx = (radj.size() > 1 ? radj[1].first : 0);

	    for(auto [d, v]: radj){
            while(!pila.empty() && mx >= dist[u] - dist[pila.back()]){
                if(--cnt[typ[pila.back()]] == 0) resact--;
                pila.pop_back();
            }

            pila.push_back(u);
            if(++cnt[typ[u]] == 1) resact++;
            
            ndfs(v, u);
            if(!pila.empty() && pila.back() == u){
                pila.pop_back();
                if(--cnt[typ[u]] == 0) resact--;
            }
            
            mx = max(mx, d);
	    }
		while(!pila.empty() && mx >= dist[u] - dist[pila.back()]){
			if(--cnt[typ[pila.back()]] == 0) resact--;
			pila.pop_back();
		}
	    res[u] = max(res[u], resact);
    };

    dfs(1, 0, 0);

    int d1 = max_element(dist.begin(), dist.end()) - dist.begin();
    dfs(d1, 0, 0);
    ndfs(d1, -1);
    int d2 = max_element(dist.begin(), dist.end()) - dist.begin();
    dfs(d2, -1, 0); 
    ndfs(d2, -1);
    for(int i = 1; i <= n; i++) cout << res[i] << "\n";
}

컴파일 시 표준 에러 (stderr) 메시지

joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:10: error: 'bits' was not declared in this scope
    1 | include <bits/stdc++.h>
      |          ^~~~
joi2019_ho_t5.cpp:1:15: error: 'stdc' was not declared in this scope; did you mean 'std'?
    1 | include <bits/stdc++.h>
      |               ^~~~
      |               std
joi2019_ho_t5.cpp:1:1: error: 'include' does not name a type
    1 | include <bits/stdc++.h>
      | ^~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:5:5: error: 'ios_base' has not been declared
    5 |     ios_base::sync_with_stdio(false);
      |     ^~~~~~~~
joi2019_ho_t5.cpp:6:5: error: 'cin' was not declared in this scope
    6 |     cin.tie(0); cout.tie(0);
      |     ^~~
joi2019_ho_t5.cpp:6:17: error: 'cout' was not declared in this scope
    6 |     cin.tie(0); cout.tie(0);
      |                 ^~~~
joi2019_ho_t5.cpp:10:12: error: 'vector' was not declared in this scope
   10 |     vector<vector<int>> adj(n + 1);
      |            ^~~~~~
joi2019_ho_t5.cpp:10:19: error: expected primary-expression before 'int'
   10 |     vector<vector<int>> adj(n + 1);
      |                   ^~~
joi2019_ho_t5.cpp:13:9: error: 'adj' was not declared in this scope
   13 |         adj[u].push_back(v);
      |         ^~~
joi2019_ho_t5.cpp:17:12: error: expected primary-expression before 'int'
   17 |     vector<int> typ(n + 1);
      |            ^~~
joi2019_ho_t5.cpp:18:41: error: 'typ' was not declared in this scope
   18 |     for (int i = 1; i <= n; ++i) cin >> typ[i];
      |                                         ^~~
joi2019_ho_t5.cpp:20:12: error: expected primary-expression before 'int'
   20 |     vector<int> dist(n + 1), len(n + 1), res(n + 1, 0);
      |            ^~~
joi2019_ho_t5.cpp:22:5: error: 'function' was not declared in this scope; did you mean 'union'?
   22 |     function<void(int, int, int)> dfs = [&](int u, int p, int d){
      |     ^~~~~~~~
      |     union
joi2019_ho_t5.cpp:22:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   22 |     function<void(int, int, int)> dfs = [&](int u, int p, int d){
      |                                ^
joi2019_ho_t5.cpp:22:14: error: expected primary-expression before 'void'
   22 |     function<void(int, int, int)> dfs = [&](int u, int p, int d){
      |              ^~~~
joi2019_ho_t5.cpp:32:12: error: expected primary-expression before 'int'
   32 |     vector<int> cnt(m + 1);
      |            ^~~
joi2019_ho_t5.cpp:34:12: error: expected primary-expression before 'int'
   34 |     vector<int> pila;
      |            ^~~
joi2019_ho_t5.cpp: In lambda function:
joi2019_ho_t5.cpp:37:15: error: 'cnt' was not declared in this scope; did you mean 'int'?
   37 |         if (++cnt[typ[u]] == 1) resact++;
      |               ^~~
      |               int
joi2019_ho_t5.cpp:37:19: error: 'typ' was not declared in this scope
   37 |         if (++cnt[typ[u]] == 1) resact++;
      |                   ^~~
joi2019_ho_t5.cpp: In lambda function:
joi2019_ho_t5.cpp:40:15: error: 'cnt' was not declared in this scope; did you mean 'int'?
   40 |         if (--cnt[typ[u]] == 0) resact--;
      |               ^~~
      |               int
joi2019_ho_t5.cpp:40:19: error: 'typ' was not declared in this scope
   40 |         if (--cnt[typ[u]] == 0) resact--;
      |                   ^~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:43:27: error: expression list treated as compound expression in functional cast [-fpermissive]
   43 |     function<void(int, int)> ndfs = [&](int u, int p){
      |                           ^
joi2019_ho_t5.cpp:43:14: error: expected primary-expression before 'void'
   43 |     function<void(int, int)> ndfs = [&](int u, int p){
      |              ^~~~
joi2019_ho_t5.cpp:75:5: error: 'dfs' was not declared in this scope
   75 |     dfs(1, 0, 0);
      |     ^~~
joi2019_ho_t5.cpp:77:26: error: 'dist' was not declared in this scope
   77 |     int d1 = max_element(dist.begin(), dist.end()) - dist.begin();
      |                          ^~~~
joi2019_ho_t5.cpp:77:14: error: 'max_element' was not declared in this scope
   77 |     int d1 = max_element(dist.begin(), dist.end()) - dist.begin();
      |              ^~~~~~~~~~~
joi2019_ho_t5.cpp:79:5: error: 'ndfs' was not declared in this scope
   79 |     ndfs(d1, -1);
      |     ^~~~
joi2019_ho_t5.cpp:83:41: error: 'res' was not declared in this scope
   83 |     for(int i = 1; i <= n; i++) cout << res[i] << "\n";
      |                                         ^~~