제출 #145004

#제출 시각아이디문제언어결과실행 시간메모리
145004evpipisCat in a tree (BOI17_catinatree)C++14
100 / 100
105 ms20344 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back typedef pair<int, int> ii; const int len = 2e5+5; int n, d, dis[len], opt[len]; vector<int> adj[len]; bool comp(int a, int b){ return (dis[a] < dis[b]); } void dfs(int u){ //printf("dfs: u = %d\n", u); for (int j = 0; j < adj[u].size(); j++){ int v = adj[u][j]; dfs(v); dis[v]++; } sort(adj[u].begin(), adj[u].end(), comp); opt[u] = 1; dis[u] = 0; int sum = 0; for (int j = 0; j < adj[u].size(); j++){ int v = adj[u][j]; opt[u] += opt[v]; if (dis[v] < d) opt[u]--; sum += opt[v]; } for (int i = (int)adj[u].size()-1, j = 0; i >= 0; i--){ int v = adj[u][i]; while (j < adj[u].size() && dis[adj[u][j]] < d-dis[v]) j++; int dis2 = dis[v], opt2 = sum-i; if (j > i) opt2 = sum-(j-1); if (mp(opt2, dis2) > mp(opt[u], dis[u])) opt[u] = opt2, dis[u] = dis2; } } int main(){ scanf("%d %d", &n, &d); for (int i = 1; i < n; i++){ int temp; scanf("%d", &temp); adj[temp].pb(i); } dfs(0); printf("%d\n", opt[0]); return 0; }

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

catinatree.cpp: In function 'void dfs(int)':
catinatree.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
catinatree.cpp:31:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
catinatree.cpp:42:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (j < adj[u].size() && dis[adj[u][j]] < d-dis[v])
                ~~^~~~~~~~~~~~~~~
catinatree.cpp: In function 'int main()':
catinatree.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &d);
     ~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &temp);
         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...