C: The number of steps in the topic seems to be many, in fact, the maximum growth of about 250 steps, because the number of steps to move 1 + 2 + 3 +. N, so there is probably only sqrt (n) step, so dp[i][j] is in the I position, growth is the value of J step, then transfer can
D: This problem in fact for a unicom block, at most only need n edge, at least to n-1, then the condition of judgment, is this unicom block whether there is a ring, the use of topological sort to be sentenced
Code:
C:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int n = 30005;int N, D , Cnt[n], dp[n][500];int dfs (int u, int cha) { if (U > 30000) return 0; if (Dp[u][cha]! =-1) return Dp[u][cha]; int tmp = d + cha-250; Dp[u][cha] = Cnt[u]; int ans = 0; if (tmp > 1) ans = max (ans, dfs (U + tmp-1, cha-1)); ans = max (ans, DFS (U + tmp, cha)); ans = max (ans, DFS (U + tmp + 1, cha + 1)); Dp[u][cha] + = ans; return Dp[u][cha];} int main () { scanf ("%d%d", &n, &d); int tmp; for (int i = 0; i < n; i++) {scanf ("%d", &tmp); cnt[tmp]++; } Memset (DP,-1, sizeof (DP)); printf ("%d\n", Dfs (d, +)); return 0;}
D:
#include <cstdio> #include <cstring> #include <vector> #include <queue>using namespace std; const int n = 100005;int n, m, Du[n], vis[n], have[n], hn;vector<int> g[n], g2[n];void dfs (int u) {have[hn++] = U Vis[u] = 1; for (int i = 0; i < g[u].size (); i++) {int v = g[u][i]; if (Vis[v]) continue; DFS (v); }}bool find () {queue<int> Q; for (int i = 0; i < HN; i++) if (!du[have[i]]) Q.push (Have[i]); while (! Q.empty ()) {int u = q.front (); Q.pop (); int sz = G2[u].size (); for (int i = 0; i < sz; i++) {int v = g2[u][i]; du[v]--; if (!du[v]) Q.push (v); }} for (int i = 0; i < HN; i++) if (Du[have[i]) return true; return false;} int main () {scanf ("%d%d", &n, &m); int u, v; while (m--) {scanf ("%d%d", &u, &v); du[v]++; G[u].push_back (v); G[v].push_back (U); G2[u].push_back (v); } int ans = n; for (int i = 1; I <= n; i++) {if (!vis[i]) {HN = 0; DFS (i); if (!find ()) ans--; }} printf ("%d\n", ans); return 0;}
Codeforces Round #286 (Div. 1) C, D