Submission #242271

#TimeUsernameProblemLanguageResultExecution timeMemory
242271shenxyMagic Tree (CEOI19_magictree)C++11
37 / 100
1284 ms791416 KiB
#include <cstdio> #include <algorithm> #include <vector> #include <cstring> #include <map> using namespace std; typedef pair<int, int> ii; int n, m, k, d[100000]; long long dpt1[100000][22], w[100000]; vector<int> adjlist[100000]; long long dp1(int v, int x) { if (dpt1[v][x] != -1) return dpt1[v][x]; if (x == 0) return dpt1[v][x] = 0; dpt1[v][x] = (d[v] == x ? w[v] : 0); for (int i: adjlist[v]) dpt1[v][x] += dp1(i, x); return dpt1[v][x] = max(dpt1[v][x], dp1(v, x - 1)); } int cnt = 1; map<int, ii> mp; vector<int> adjlist2[1001]; void dfs(int v, int p) { d[0] = w[0] = 0; if (mp.find(v) != mp.end()) { d[cnt] = mp[v].first; w[cnt] = mp[v].second; adjlist2[p].push_back(cnt); p = cnt; ++cnt; } for (int i: adjlist[v]) dfs(i, p); } long long dpt2[1001][100002]; long long dp2(int v, int x) { if (dpt2[v][x] != -1) return dpt2[v][x]; if (x == 0) return dpt2[v][x] = 0; dpt2[v][x] = (d[v] == x ? w[v] : 0); for (int i: adjlist2[v]) dpt2[v][x] += dp2(i, x); return dpt2[v][x] = max(dpt2[v][x], dp2(v, x - 1)); } int main() { scanf("%d %d %d", &n, &m, &k); if (k <= 20) { int p, v, D, W; for (int i = 1; i < n; ++i) { scanf("%d", &p); adjlist[p - 1].push_back(i); } fill_n(d, 100000, 0); fill_n(w, 100000, 0); for (int i = 0; i < m; ++i) { scanf("%d %d %d", &v, &D, &W); d[v - 1] = D; w[v - 1] = W; } memset(dpt1, -1, sizeof dpt1); printf("%lld", dp1(0, k + 1)); } else if (m <= 1000) { int p, v, d, w; for (int i = 1; i < n; ++i) { scanf("%d", &p); adjlist[p - 1].push_back(i); } for (int i = 0; i < m; ++i) { scanf("%d %d %d", &v, &d, &w); mp[v] = ii(d, w); } memset(dpt2, -1, sizeof dpt2); dfs(0, 0); printf("%lld", dp2(0, k + 1)); } else { for (int i = 1; i < n; ++i) scanf("%d", &k); int v, d, w; long long ans = 0; for (int i = 0; i < m; ++i) { scanf("%d %d %d", &v, &d, &w); ans += w; } printf("%lld", ans); } return 0; }

Compilation message (stderr)

magictree.cpp: In function 'int main()':
magictree.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &k);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:45:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &p);
    ~~~~~^~~~~~~~~~
magictree.cpp:51:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &v, &D, &W);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:60:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &p);
    ~~~~~^~~~~~~~~~
magictree.cpp:64:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &v, &d, &w);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:71:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   for (int i = 1; i < n; ++i) scanf("%d", &k);
                               ~~~~~^~~~~~~~~~
magictree.cpp:75:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &v, &d, &w);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...