湖南集训DAY3
思路:
1 #include <cstdio> 2 #include <cctype> 3 #include <cstring> 4 5 const int MAXN=1000010; 6 7 int n,k; 8 9 char s[MAXN]; 10 11 int hh() { 12 freopen("trans.in","r",stdin); 13 freopen("trans.out","w",stdout); 14 while(scanf("%d%d",&n,&k)!=EOF) { 15 scanf("%s",s+1); 16 for(int i=1; i<=n; ++i) { 17 if(k && s[i]=='2' && s[i+1]=='3') { 18 if(i&1) s[i+1]='2'; 19 else s[i]='3'; 20 --k;--i; 21 } 22 else if(s[i]=='2' && s[i+1]=='2' && s[i+2]=='3' && (i&1)) k%=2; 23 } 24 for(int i=1;i<=n;++i) printf("%c",s[i]); 25 printf("\n"); 26 } 27 return 0; 28 } 29 30 int sb=hh(); 31 int main(int argc,char**argv) {;}
思路:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int n, a[205][5], m, x, y, f[205][10005][5] = {{{0}}}; 4 int g[10005][5][5] = {{{0}}}, maxi, ans = 0; 5 bool flag[205][4] = {{false}}; 6 int Main() 7 { 8 freopen("snakevsblock.in","r",stdin); 9 freopen("snakevsblock.out","w",stdout); 10 scanf("%d", &n); 11 for (int i = 1; i <= n; i++) 12 for (int j = 0; j < 5; j++) 13 scanf("%d", &a[i][j]); 14 scanf("%d", &m); 15 for (int i = 1; i <= m; i++) 16 { 17 scanf("%d%d", &x, &y); 18 flag[x][y - 1] = true; 19 } 20 memset(f, -0x7f7f7f, sizeof f); 21 f[0][4][2] = 0; 22 maxi = n * 50; 23 for (int i = 1; i <= n; i++) 24 { 25 memset(g, -0x7f7f7f, sizeof g); 26 for (int j = 0; j <= maxi; j++) 27 for (int k = 0; k < 5; k++) 28 if (j - a[i][k] >= 0 && j - a[i][k] <= maxi) 29 f[i][j][k] = g[j][k][k] = f[i - 1][j - a[i][k]][k] + max(-a[i][k], 0); 30 for (int l = 1; l <= 4; l++) 31 for (int j = 0, k = j + l; k < 5; j++, k++) 32 for (int v = 0, val; v <= maxi; v++) 33 { 34 if (!flag[i][j] && (val = v - a[i][j]) >= 0 && val <= maxi) g[v][j][k] = g[val][j + 1][k] + max(-a[i][j], 0); 35 if (!flag[i][k - 1] && (val = v - a[i][k]) >= 0 && val <= maxi) g[v][j][k] = max(g[v][j][k], g[val][j][k - 1] + max(-a[i][k], 0)); 36 for (int to = j; to <= k; to++) f[i][v][to] = max(f[i][v][to], g[v][j][k]); 37 } 38 } 39 for (int l = 0; l <= n; l++) 40 for (int i = 0; i <= maxi; i++) 41 for (int j = 0; j < 5; j++) 42 ans = max(ans, f[l][i][j]); 43 printf("%d\n", ans); 44 return 0; 45 } 46 int dawn = Main (); 47 int main (int argc, char *argv[]) {;}
思路:LCA求每一条链的交集 50分
正解
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<vector> 6 #define MAXN 100005 7 #define pb push_back 8 using namespace std; 9 10 struct Edge{ int to,next; }e[MAXN << 1]; 11 int head[MAXN],tot,n,m,dep[MAXN],fa[MAXN][20],Ans; 12 vector<int> vec[300000]; 13 14 inline void read(int &x){ 15 x=0; register char c = getchar(); 16 while(c > '9' || c < '0') c = getchar(); 17 while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } 18 } 19 20 inline void Add_Edge(int u,int v) { 21 e[++tot].to = v,e[tot].next = head[u],head[u] = tot; 22 e[++tot].to = u,e[tot].next = head[v],head[v] = tot; 23 } 24 25 void DFS(int u,int fat,int deepth) { 26 fa[u][0] = fat,dep[u] = deepth; 27 for(int i=head[u]; i; i=e[i].next) { 28 int v = e[i].to; 29 if(v == fat) continue; 30 DFS(v,u,deepth + 1); 31 } 32 } 33 34 void DP() { 35 for(int j=1; j<=18; ++j) 36 for(int i=1; i<=n; ++i) 37 fa[i][j] = fa[fa[i][j-1]][j-1]; 38 } 39 40 void LCA(int u,int v) { 41 if(dep[u] < dep[v]) swap(u,v); 42 while(dep[u] > dep[v]){ u = fa[u][0],vec[Ans].pb(u); } 43 while(u != v){ 44 u = fa[u][0], v = fa[v][0]; 45 vec[Ans].pb(u); 46 if(u != v) vec[Ans].pb(v); 47 } 48 } 49 50 void Megre() { 51 vector<int> temp; 52 for(int i=1; i<Ans; ++i) { 53 for(int j=0; j<vec[Ans].size(); ++j) 54 for(int k=0; k<vec[i].size(); ++k) 55 if(vec[i][k] == vec[Ans][j]) temp.pb(vec[Ans][j]); 56 if(!temp.empty()){ 57 vec[i].clear(),vec[Ans].clear(),Ans--; 58 for(int k=0; k<temp.size(); ++k) 59 vec[i].pb(temp[k]); 60 break; 61 } 62 } 63 } 64 65 int main(int agrc,char *argv[]) { 66 freopen("ping.in","r",stdin); 67 freopen("ping.out","w",stdout); 68 read(n),read(m); register int i; int u,v,k; 69 for(i=1; i<=m; ++i) 70 read(u),read(v),Add_Edge(u,v); 71 DFS(1,0,-1);DP(); 72 read(k); 73 for(i=1; i<=k; ++i) { 74 read(u),read(v); 75 ++Ans; 76 vec[Ans].pb(u); 77 if(u != v) vec[Ans].pb(v); 78 LCA(u,v); 79 Megre(); 80 } 81 printf("%d\n",Ans); 82 for(int i=1; i<=Ans; ++i) 83 printf("%d ",vec[i][0]); 84 fclose(stdin); fclose(stdout); 85 return 0; 86 }
1 #include <cstdio> 2 #include<algorithm> 3 using namespace std; 4 int n, m, P, u, v, to[200005], nxt[200005], p[100005], deep[100005], q[500005][2]; 5 int son[100005], fa[100005], size[100005], top[100005], dfsx[100005], cnt, ans; 6 int lca[500005], Q[500005], lr[100005][2]; 7 int sta[100005]; 8 bool flag[400005]; 9 void dfs(int x) { 10 size[x] = 1; 11 for (int i = p[x]; i != -1; i = nxt[i]) 12 if (to[i] != fa[x]) { 13 fa[to[i]] = x; 14 deep[to[i]] = deep[x] + 1; 15 dfs(to[i]); 16 if (son[x] == -1 || size[to[i]] > size[son[x]]) son[x] = to[i]; 17 size[x] += size[to[i]]; 18 } 19 } 20 void dfs1(int x) { 21 dfsx[x] = ++cnt; 22 if (son[x] != -1) top[son[x]] = top[x], dfs1(son[x]); 23 for (int i = p[x]; i != -1; i = nxt[i]) 24 if (to[i] != fa[x] && to[i] != son[x]) 25 top[to[i]] = to[i], dfs1(to[i]); 26 } 27 int findlca(int x, int y) { 28 while (1) { 29 if (top[x] == top[y]) return deep[x] > deep[y]? y : x; 30 if (deep[top[x]] > deep[top[y]]) x = fa[top[x]]; 31 else y = fa[top[y]]; 32 } 33 } 34 bool query(int x, int l, int r, int ll, int rr) { 35 if (l == ll && r == rr) return flag[x]; 36 int mid = (l + r) >> 1, L = x << 1, R = L | 1; 37 if (rr <= mid) return query(L, l, mid, ll, rr); 38 else if (ll > mid) return query(R, mid + 1, r, ll, rr); 39 else return query(L, l, mid, ll, mid) | query(R, mid + 1, r, mid + 1, rr); 40 } 41 void modify(int x, int l, int r, int to) { 42 flag[x] = true; 43 if (l == r) return; 44 int mid = (l + r) >> 1, L = x << 1, R = L | 1; 45 if (to <= mid) modify(L, l, mid, to); 46 else modify(R, mid + 1, r, to); 47 } 48 bool Query(int x, int y) { 49 while (1) { 50 if (top[x] == top[y]) { 51 if (deep[x] < deep[y]) return query(1, 1, cnt, dfsx[x], dfsx[y]); 52 else return query(1, 1, cnt, dfsx[y], dfsx[x]); 53 } 54 if (deep[top[x]] > deep[top[y]]) 55 if (query(1, 1, cnt, dfsx[top[x]], dfsx[x])) return true; 56 else x = fa[top[x]]; 57 else { 58 if (query(1, 1, cnt, dfsx[top[y]], dfsx[y])) return true; 59 else y = fa[top[y]]; 60 } 61 } 62 } 63 void work(int x) { 64 for (int i = p[x]; i != -1; i = nxt[i]) 65 if (to[i] != fa[x]) 66 work(to[i]); 67 for (int i = lr[x][0]; i <= lr[x][1]; i++) 68 if (!Query(q[Q[i]][0], q[Q[i]][1])) { 69 modify(1, 1, cnt, dfsx[x]); 70 sta[++ans] = x; 71 return; 72 } 73 } 74 bool cmp(int x, int y) { 75 return lca[x] < lca[y]; 76 } 77 int main() { 78 freopen("ping.in","r",stdin); 79 freopen("ping.out","w",stdout); 80 scanf("%d%d", &n, &m); 81 for (int i = 0; i <= n; i++) p[i] = son[i] = -1, top[i] = size[i] = fa[i] = deep[i] = 0; 82 for (int i = 1; i <= n * 4; i++) flag[i] = false; 83 for (int i = 1; i <= m; i++) { 84 scanf("%d%d", &u, &v); 85 u--, v--; 86 to[i * 2 - 1] = v; 87 nxt[i * 2 - 1] = p[u]; 88 p[u] = i * 2 - 1; 89 to[i * 2] = u; 90 nxt[i * 2] = p[v]; 91 p[v] = i * 2; 92 } 93 deep[0] = 1; 94 dfs(0); 95 cnt = 0; 96 dfs1(0); 97 scanf("%d", &P); 98 for (int i = 1; i <= P; i++) { 99 scanf("%d%d", &u, &v); 100 u--, v--; 101 q[i][0] = u, q[i][1] = v; 102 lca[i] = findlca(u, v); 103 Q[i] = i; 104 } 105 sort(Q + 1, Q + P + 1, cmp); 106 for (int i = 0; i <= n; i++) lr[i][0] = P + 1, lr[i][1] = 0; 107 for (int i = 1; i <= P; i++) { 108 lr[lca[Q[i]]][0] = min(lr[lca[Q[i]]][0], i); 109 lr[lca[Q[i]]][1] = max(lr[lca[Q[i]]][1], i); 110 } 111 ans = 0; 112 work(0); 113 printf("%d\n", ans); 114 for (int i = 1; i <= ans; i++) printf("%d ", sta[i] + 1); 115 return 0; 116 }
作者:乌鸦坐飞机
出处:http://www.cnblogs.com/whistle13326/
新的风暴已经出现
怎么能够停止不前
穿越时空 竭尽全力
我会来到你身边
微笑面对危险
梦想成真不会遥远
鼓起勇气 坚定向前
奇迹一定会出现