Submission #201604

#TimeUsernameProblemLanguageResultExecution timeMemory
201604SenseiBitaro’s Party (JOI18_bitaro)C++17
7 / 100
2071 ms10872 KiB
/* DATE: 2020-02-11 14:19:26 NAME: PROBLEM: JOI18_BITARO */ #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; const int SQRT = 350; vector<int> edge[MAXN + 7]; vector<int> redge[MAXN + 7]; int dp[MAXN + 7]; bool vis[MAXN + 7]; vector<pair<int, int> > sqrt_furthest[MAXN + 7]; void sqrt_dfs(int u) { vis[u] = true; for (int i = 0; i < redge[u].size(); i++) { int v = redge[u][i]; if (!vis[v]) { sqrt_dfs(v); } } vector<pair<int, int> > ans; ans.push_back({0, u}); for (int i = 0; i < redge[u].size(); i++) { int v = redge[u][i]; vector<pair<int, int> > copy_ans; swap(ans, copy_ans); // copy_ans.push_back({1, v}); set<int> in; int x = 0; int y = 0; while (ans.size() < SQRT and x < sqrt_furthest[v].size() and y < copy_ans.size()) { if (in.find(sqrt_furthest[v][x].second) != in.end()) { x++; continue; } if (in.find(copy_ans[y].second) != in.end()) { y++; continue; } if (sqrt_furthest[v][x].first + 1 > copy_ans[y].first) { ans.push_back({sqrt_furthest[v][x].first + 1, sqrt_furthest[v][x].second}); in.insert(sqrt_furthest[v][x].second); } else { ans.push_back(copy_ans[y]); in.insert(copy_ans[y].second); } } while (ans.size() < SQRT and x < sqrt_furthest[v].size()) { if (in.find(sqrt_furthest[v][x].second) != in.end()) { x++; continue; } ans.push_back({sqrt_furthest[v][x].first + 1, sqrt_furthest[v][x].second}); in.insert(sqrt_furthest[v][x].second); } while (ans.size() < SQRT and y < copy_ans.size()) { if (in.find(copy_ans[y].second) != in.end()) { y++; continue; } ans.push_back(copy_ans[y]); in.insert(copy_ans[y].second); } } sqrt_furthest[u] = ans; } void naive_dfs(int u, vector<int> &disallowed) { vis[u] = true; if (binary_search(disallowed.begin(), disallowed.end(), u) == true) { dp[u] = -1; } else { dp[u] = 0; } for (int i = 0; i < redge[u].size(); i++) { int v = redge[u][i]; if (!vis[v]) { naive_dfs(v, disallowed); } if (dp[v] != -1) { dp[u] = max(dp[u], 1 + dp[v]); } } } int main() { int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); edge[u].push_back(v); redge[v].push_back(u); } for (int u = 1; u <= n; u++) { if (!vis[u]) { sqrt_dfs(u); } } for (int i = 1; i <= q; i++) { int u; scanf("%d", &u); int cnt; scanf("%d", &cnt); vector<int> disallowed(cnt); for (int j = 0; j < cnt; j++) { scanf("%d", &disallowed[j]); } if (cnt >= SQRT) { memset(dp, 0, sizeof dp); memset(vis, 0, sizeof vis); naive_dfs(u, disallowed); printf("%d\n", dp[u]); } else { int ans = -1; for (int j = 0; j < sqrt_furthest[u].size(); j++) { if (binary_search(disallowed.begin(), disallowed.end(), sqrt_furthest[u][j].second) == false) { ans = sqrt_furthest[u][j].first; break; } } printf("%d\n", ans); } } return 0; }

Compilation message (stderr)

bitaro.cpp: In function 'void sqrt_dfs(int)':
bitaro.cpp:23:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < redge[u].size(); i++) {
                   ~~^~~~~~~~~~~~~~~~~
bitaro.cpp:31:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < redge[u].size(); i++) {
                   ~~^~~~~~~~~~~~~~~~~
bitaro.cpp:39:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (ans.size() < SQRT and x < sqrt_furthest[v].size() and y < copy_ans.size()) {
                                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:39:68: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (ans.size() < SQRT and x < sqrt_furthest[v].size() and y < copy_ans.size()) {
                                                                  ~~^~~~~~~~~~~~~~~~~
bitaro.cpp:57:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (ans.size() < SQRT and x < sqrt_furthest[v].size()) {
                                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:65:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (ans.size() < SQRT and y < copy_ans.size()) {
                                  ~~^~~~~~~~~~~~~~~~~
bitaro.cpp: In function 'void naive_dfs(int, std::vector<int>&)':
bitaro.cpp:85:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < redge[u].size(); i++) {
                   ~~^~~~~~~~~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:130:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int j = 0; j < sqrt_furthest[u].size(); j++) {
                       ~~^~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &u, &v);
     ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &u);
     ~~~~~^~~~~~~~~~
bitaro.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &cnt);
     ~~~~~^~~~~~~~~~~~
bitaro.cpp:120:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", &disallowed[j]);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...