今天又爆零了,又是又,怎么又是又,爆零爆多了,又也就经常挂嘴边了,看到这句话,你一定很想说一句””,弱菜被骂傻,也很正常啦

如果你不开心,可以考虑往下看。

翻到E(HDU 4635 Strongly connected)题,这么短的题目,肯定要先看啦。然后D(LightOJ 1229),然后C(ZOJ 2243),然后F(HDU 4711),然后B(CodeForces 385D),然后看A(HDU 3889)好吧,我承认,A题看了一眼就不看了,B题一看是线段什么有点几何的味道就果断放弃,然后C题,傻傻的理解错题意,提交一直WA,然后没办法,看E题,想到只要保证最后至少两个连通分量,就可以满足题意,然后要求最大值,那就保证有且仅有两个连通分量就可以了,对于一个连通分量最多只能有x(x-1)边, x表示顶点数 ,然后得出一个式子,边数f = n*n-n-1+x*x-(n+1)x;当x更(n+1)/2的差值越大,f越大,换句话说,只要把一个连通分量顶点个数最小的独立出来,把其它的连通分量都合并成一个连通分量就可以了,

可是我没考虑下面这种情况

这时候如果把3独立出来,5、9、7弄成一个连通分量,那么3也会跟5,9,7合并成一个连通分量,所以不能选3,

最小的不能选,那就选5吧,把3、7、9合并,可以。

也就是说是要把顶点个数尽量小且入度或者初度为零(一个连通分量看成一个点)的连通分量独立出来。

  1. view code#include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <vector>
  8. #include <map>
  9. #include <stack>
  10. using namespace std;
  11. typedef long long ll;
  12. const int INF = 0x3f3f3f3f;
  13. const int N = 100010;
  14. int _, cas=1, n, m;
  15. int in[N], out[N], num[N];
  16.  
  17. vector<int > G[N];
  18. int pre[N], lowlink[N], dfs_clock, scc_cnt, sccno[N];
  19. stack<int >S;
  20.  
  21. void dfs(int u)
  22. {
  23. pre[u] = lowlink[u] = ++dfs_clock;
  24. S.push(u);
  25. int siz = G[u].size();
  26. for(int i=0; i<siz; i++)
  27. {
  28. int v = G[u][i];
  29. if(!pre[v])
  30. {
  31. dfs(v);
  32. lowlink[u] = min(lowlink[u], lowlink[v]);
  33. }
  34. else if(!sccno[v])
  35. {
  36. lowlink[u] = min(lowlink[u], pre[v]);
  37. }
  38. }
  39. if(lowlink[u] == pre[u])
  40. {
  41. scc_cnt++;
  42. for(;;)
  43. {
  44. int x = S.top(); S.pop();
  45. sccno[x] = scc_cnt;
  46. num[scc_cnt]++;
  47. if(x==u) break;
  48. }
  49. }
  50. }
  51.  
  52. void find_scc()
  53. {
  54. dfs_clock = 0;
  55. scc_cnt = 0;
  56. memset(sccno, 0, sizeof(sccno));
  57. memset(pre, 0, sizeof(pre));
  58. for(int i=1; i<=n; i++)
  59. {
  60. if(!pre[i]) dfs(i);
  61. }
  62. }
  63.  
  64. void solve()
  65. {
  66. scanf("%d%d", &n ,&m);
  67. memset(num, 0, sizeof(num));
  68. memset(in, 0, sizeof(in));
  69. memset(out, 0, sizeof(out));
  70. for(int i=1; i<=n ;i++) G[i].clear();
  71.  
  72. int u, v;
  73. for(int i=0; i<m; i++)
  74. {
  75. scanf("%d%d", &u, &v);
  76. G[u].push_back(v);
  77. }
  78. find_scc();
  79. printf("Case %d: ", cas++);
  80. if(scc_cnt==1)
  81. {
  82. printf("-1\n");
  83. return ;
  84. }
  85. ll ans = 0, Min = INF;
  86. for(int i=1; i<=n; i++)
  87. {
  88. int siz = G[i].size();
  89. for(int j=0; j<siz; j++)
  90. {
  91. if(sccno[i]!=sccno[G[i][j]])
  92. {
  93. in[sccno[G[i][j]]]++;
  94. out[sccno[i]]++;
  95. }
  96. }
  97. }
  98. for(int i=1; i<=scc_cnt; i++)
  99. {
  100. if((in[i]==0 || out[i]==0) && Min>num[i]) Min = num[i];
  101. // printf("num[%d] = %d\n", i, num[i]);
  102. // printf("out = %d, in = %d\n", out[i], in[i]);
  103. }
  104. ans = (Min-1)*Min- m + (n-Min)*(n-Min-1)+Min*(n-Min);
  105. cout<<ans<<endl;
  106. }
  107.  
  108. int main()
  109. {
  110. // freopen("in", "r", stdin);
  111. cin>>_;
  112. while(_--) solve();
  113. return 0;
  114. }

红色部分就是思维漏洞

。差一点,不过acm没有差一点,只有ac或者没ac.

下面再来总结一下题目吧
Problem A
HDU 3889(水题,不会做)

Problem B
CodeForces 385D(dp,题意尚不明确)

Problem C
ZOJ 2243(什么treap,被坑)

笛卡尔树:

  每个节点有2个关键字key、value。从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大;从value的角度看,这是一个堆。

题意:以字符串为关键字key,数字为关键字value,构造一个二叉搜索大堆,最后按要求中序遍历 笛卡尔树的构造。

  1. view code#include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <vector>
  7. using namespace std;
  8. #define lson l,m,rt<<1
  9. #define rson m+1,r,rt<<1|1
  10. const int N = 55555;
  11. const int INF = 1<<30;
  12. int n, pos[N<<2], Max[N<<2];
  13.  
  14. struct node
  15. {
  16. char str[55];
  17. int data;
  18. bool operator < (const node &o) const{
  19. return strcmp(str,o.str)<0;
  20. }
  21. }sto[N];
  22.  
  23. void Up(int rt)
  24. {
  25. int ls = rt<<1, rs=ls|1;
  26. if(Max[rs]>Max[ls]) pos[rt] = pos[rs], Max[rt] = Max[rs];
  27. else pos[rt] = pos[ls], Max[rt] = Max[ls];
  28. }
  29.  
  30. void build(int l, int r, int rt)
  31. {
  32. if(l==r)
  33. {
  34. Max[rt] = sto[l].data;
  35. pos[rt] = l;
  36. return ;
  37. }
  38. int m = (l+r)>>1;
  39. build(lson);
  40. build(rson);
  41. Up(rt);
  42. }
  43.  
  44. int query(int L, int R, int l, int r, int rt)
  45. {
  46. if(L<=l && R>=r) return pos[rt];
  47. int m = (l+r)>>1;
  48. if(R<=m) return query(L, R, lson);
  49. if(L>m) return query(L, R, rson);
  50. int lpos = query(L, R, lson);
  51. int rpos = query(L, R, rson);
  52. return sto[lpos].data<sto[rpos].data?rpos:lpos;
  53. }
  54.  
  55. void print(int l, int r)
  56. {
  57. if(l>r) return ;
  58. if(l==r)
  59. {
  60. printf("(%s/%d)", sto[l].str, sto[l].data);
  61. return ;
  62. }
  63. int m = query(l, r, 0, n-1, 1);
  64. printf("(");
  65. print(l, m-1);
  66. printf("%s/%d", sto[m].str, sto[m].data);
  67. print(m+1,r);
  68. printf(")");
  69. }
  70.  
  71. void solve()
  72. {
  73. for(int i=0; i<n; i++)
  74. {
  75. scanf(" %[a-z]/%d", sto[i].str, &sto[i].data);//这个输入方式。。又涨姿势了
  76. // printf("%s/%d\n", sto[i].str, sto[i].data);
  77. }
  78. sort(sto, sto+n);
  79. build(0, n-1, 1);
  80. print(0, n-1);
  81. printf("\n");
  82. }
  83.  
  84. int main()
  85. {
  86. // freopen("in.txt", "r", stdin);
  87. while(scanf("%d", &n)>0 && n) solve();
  88. return 0;
  89. }
  1. //[a-z]表示读取的字符串由a-z中的字符组成,其余的字符为定界符scanf/fscanf 的%[]和%n使用方法

Problem D
LightOJ 1229(博弈,大白书P139)

  1. view code#include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. const int N = 222;
  7. int _, cas=1, n;
  8. int sg[N] = {0, 1, 1, 1};
  9. bool vis[N];
  10. char s[N];
  11.  
  12. void init()
  13. {
  14. int i, j;
  15. for(i=4; i<=200; i++)
  16. {
  17. memset(vis, 0, sizeof(vis));
  18. for(j=3; j<=5 && j<=i ; j++) vis[sg[i-j]] = 1;
  19. for(j=1; j+5<i; j++) vis[(sg[j]^sg[i-j-5])] = 1;
  20. for(j=0; ; j++) if(!vis[j]) break;
  21. sg[i] = j;
  22. }
  23. // for(int i=0;i <20; i++) printf("sg[%d] = %d\n", i, sg[i]);
  24. }
  25.  
  26. bool is_ok(int pos)
  27. {
  28. if(pos>2 && s[pos-2]=='X' &&s[pos-1]=='X') return true;
  29. if(pos>1 && pos<n && s[pos-1]=='X' && s[pos+1]=='X') return true;
  30. if(pos<n-1 && s[pos+1]=='X' && s[pos+2] == 'X') return true;
  31. for(int k=pos-2; k<=n && k<=pos+2; k++)
  32. {
  33. if(k<1) continue;
  34. if(s[k]=='X') return 0;
  35. }
  36. return 1;
  37. }
  38.  
  39. bool win(int pos)
  40. {
  41. for(int i=3; i<=n; i++) if(s[i]=='X'&&s[i-1]=='X'&&s[i-2]=='X') return true;
  42. for(int i=1; i<=n; i++)
  43. {
  44. if(i>2 && s[i-2]=='X' &&s[i-1]=='X') return 0;
  45. if(i>1 && i<n && s[i-1]=='X' && s[i+1]=='X') return 0;
  46. if(i<n-1 && s[i+1]=='X' && s[i+2] == 'X') return 0;
  47. }
  48. int ans = 0, pre = 1;
  49. for( ; ; )
  50. {
  51. while(pre<=n && s[pre]=='X') pre++;
  52. if(pre>n) break;
  53. int k = pre;
  54. while(k<=n && s[k]=='.') k++;
  55. if(k<=n && pre>1 && s[pre-1]=='X'){
  56. if(s[k]=='X' && k-pre-4>0) ans ^= sg[k-pre-4];
  57. }
  58. else if(k-pre-2>0) ans ^= sg[k-pre-2];
  59. if(k>n) break;
  60. pre = k;
  61. }
  62. return ans==0;
  63. }
  64.  
  65. void solve()
  66. {
  67. scanf("%s", s+1);
  68. n = strlen(s+1);
  69.  
  70. printf("Case %d:", cas++);
  71. int f = 1;
  72. for(int i=1; i<=n; i++)
  73. {
  74. if(!is_ok(i)) continue;
  75. s[i] = 'X';
  76. if(win(i))
  77. printf(" %d", i), f = 0;
  78. s[i] = '.';
  79. }
  80. if(f) printf("");
  81. puts("");
  82. }
  83.  
  84. int main()
  85. {
  86. // freopen("in.txt", "r", stdin);
  87. init();
  88. cin>>_;
  89. while(_--) solve();
  90. return 0;
  91. }

Problem E
HDU 4635(。。。。。。。。。。。。。。。。。,此处省略一万字)

Problem F
HDU 4711 。。

爆零后的感受外加一道强联通分量HDU 4635的题解的更多相关文章

  1. POJ 2186 Popular Cows(强联通分量)

    题目链接:http://poj.org/problem?id=2186 题目大意:    每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...

  2. Kosaraju算法---强联通分量

    1.基础知识 所需结构:原图.反向图(若在原图中存在vi到vj有向边,在反向图中就变为vj到vi的有向边).标记数组(标记是否遍历过).一个栈(或记录顶点离开时间的数组).      算法描叙: :对 ...

  3. [CF #236 (Div. 2) E] Strictly Positive Matrix(强联通分量)

    题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1 ...

  4. 洛谷 P2661 信息传递 Label:并查集||强联通分量

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  5. POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)

    题目链接:http://poj.org/problem?id=2186 题目大意:有n头牛和m对关系, 每一对关系有两个数(a, b)代表a牛认为b牛是“受欢迎”的,且这种关系具有传递性, 如果a牛认 ...

  6. 强联通分量-tarjan算法

    定义:在一张有向图中,两个点可以相互到达,则称这两个点强连通:一张有向图上任意两个点可以相互到达,则称这张图为强连通图:非强连通图有极大的强连通子图,成为强联通分量. 如图,{1},{6}分别是一个强 ...

  7. POJ 2186 Popular cows(Kosaraju+强联通分量模板)

    题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...

  8. 【强联通分量缩点】【Tarjan】bzoj1051 [HAOI2006]受欢迎的牛

    就是看是否有一些点,从其他任何点出发都可到达 定理:有向无环图中唯一出度为0的点,一定可以由任何点出发均可达. 所以缩点,若出度为零的点(强联通分量)唯一,则答案为该强联通分量中点的度数. 若不唯一, ...

  9. 培训补坑(day2:割点与桥+强联通分量)

    补坑ing... 好吧,这是第二天. 这一天我们主要围绕的就是一个人:tarjan......创造的强联通分量算法 对于这一天的内容我不按照顺序来讲,我们先讲一讲强联通分量,然后再讲割点与桥会便于理解 ...

随机推荐

  1. android sdk 镜像

    大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn ...

  2. MAC OS X 系统怎么样?

    朝鲜的 IT 应用状况并不为外界所熟知,过去媒体纷纷报道,朝鲜已故领导人金正日酷爱苹果电子产品,而最近一份调查报告显示,在朝鲜个人电脑操作系统市场,苹果 MAC OS X 系统位居第一名,遥遥领先微软 ...

  3. 多余的Using Namespaces或引用会影响程序的执行效率么?

    在.NET程序编写中,需要using相应命名空间或添加相应的References,可有时候没有使用到的命名空间也被添加到了Using Namespaces中,那么,这样会影响程序的执行效率么? 通过示 ...

  4. js用正则表达式验证用户和密码的安全性,生成随机验证码

    制作了一个表单,表单验证用户.密码.随机验证码 html页面

  5. 实现GridView翻页并且实现CheckBox选中功能的保持

    在GridView与数据库进行绑定后,由得到的数据记录可能有许多条,以至一个页面无法容纳,这时需要进行多页显. 要实现分页显现,只要使用分页类 "PagedDataSource" ...

  6. 回文串---Palindrome

    POJ   3974 Description Andy the smart computer science student was attending an algorithms class whe ...

  7. Java中native的用法

    原文来自:http://blog.csdn.net/funneies/article/details/8949660 native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件, ...

  8. Wechat4j之Hello world——使用wechat4j快速开发java版微信公众号

    Wechat4j是一个开源的java微信开发框架,是目前最简单易用的java微信开发框架. 项目地址:https://github.com/sword-org/wechat4j Wechat4j.ja ...

  9. 用构造函数创建对象时的this的指向问题

    用构造函数方式创建对象: function Person(name,age){ this.name=name; this.age=age; this.sayname=function(){ alert ...

  10. 解决Sharepoint每天第一次打开速度慢的问题

    每天第一次打开Sharepoint的网站会非常慢,下面是解决这个问题的几个方法. 添加crl.microsoft.com到Hosts文件,IP地址指向服务器本机. 允许服务器直接连接到crl.micr ...