A:

题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x。

题解:

首先每个点应该都在一个环上,否则无解。

对于大小为k的奇环上的点,满足要求的最小的t是k.

对于大小为k的偶环上的点,满足要求的最小的t是k/2.

对于每个环求最小公倍数即可。  数据范围很小,直接暴力求环就可以了。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <cstdlib>
  9. #include <set>
  10. using namespace std;
  11.  
  12. #define X first
  13. #define Y second
  14. #define Mod 1000000007
  15. #define N 110
  16. typedef long long ll;
  17. typedef pair<int,int> pii;
  18.  
  19. int n;
  20. int a[N];
  21. bool vis[N];
  22.  
  23. ll gcd(ll x,ll y)
  24. {
  25. ll tmp;
  26. while (y)
  27. {
  28. tmp=x%y;
  29. x=y;y=tmp;
  30. }
  31. return x;
  32. }
  33.  
  34. ll lcm(ll x,ll y)
  35. {
  36. return x/gcd(x,y)*y;
  37. }
  38.  
  39. int main()
  40. {
  41. //freopen("in.in","r",stdin);
  42. //freopen("out.out","w",stdout);
  43.  
  44. scanf("%d",&n);
  45. for (int i=;i<=n;i++) scanf("%d",&a[i]);
  46. ll ans=;
  47. for (int i=;i<=n;i++)
  48. {
  49. int t=i,c=;
  50. memset(vis,,sizeof(vis));
  51. do
  52. {
  53. vis[t]=true;
  54. c++,t=a[t];
  55. }while(!vis[t]);
  56. if (t==i)
  57. {
  58. if (!(c&)) c>>=;
  59. ans=lcm(ans,c);
  60. }
  61. else
  62. {
  63. printf("-1\n");
  64. return ;
  65. }
  66. }
  67. printf("%d\n",ans);
  68. return ;
  69. }

B:

题目大意: 你要从n个客人中邀请一些人来参加派对, 每个客人有一个w和b。要求在邀请的客人的w之和不能超过W的情况下,使得客人的b的和最大。 n,W<=1000

有一些客人是朋友关系,且满足传递性,对于一些朋友,要么全部邀请,要么最多邀请其中的一个。

题解:

考虑DP。 首先用并查集搞出朋友关系的集合,dp[i][j]表示考虑前i个集合,w的和为j的最优解。

转移的时候 要么把整个第i个集合取过来,要么枚举其中的一个元素取过来。

考虑复杂度: 对于第k个人,假设他在第i个集合,那么他在dp[i][0.....W]的时候都用来转移了一次。

所以复杂度是O(nW).

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <cstdlib>
  9. #include <set>
  10. using namespace std;
  11.  
  12. #define X first
  13. #define Y second
  14. #define Mod 1000000007
  15. #define N 1010
  16. #define M 10000010
  17. typedef long long ll;
  18. typedef pair<int,int> pii;
  19.  
  20. int n,m,w;
  21. int a[N],b[N],father[N],id[N];
  22. int s1[N],s2[N];
  23. int dp[N][N];
  24.  
  25. vector<int> g[N];
  26.  
  27. int Find(int x)
  28. {
  29. if (father[x]==x) return x;
  30. father[x]=Find(father[x]);
  31. return father[x];
  32. }
  33.  
  34. void Merge(int x,int y)
  35. {
  36. x=Find(x),y=Find(y);
  37. if (x==y) return ;
  38. father[x]=y;
  39. }
  40.  
  41. int main()
  42. {
  43. //freopen("in.in","r",stdin);
  44. //freopen("out.out","w",stdout);
  45.  
  46. scanf("%d%d%d",&n,&m,&w);
  47. for (int i=;i<=n;i++) scanf("%d",&a[i]);
  48. for (int i=;i<=n;i++) scanf("%d",&b[i]),father[i]=i;
  49. int x,y;
  50. for (int i=;i<=m;i++)
  51. {
  52. scanf("%d%d",&x,&y);
  53. Merge(x,y);
  54. }
  55. int t=;
  56. for (int i=;i<=n;i++) if (Find(i)==i) id[i]=++t;
  57. for (int i=;i<=n;i++)
  58. {
  59. int x=id[Find(i)];
  60. g[x].push_back(i);
  61. s1[x]+=a[i];
  62. s2[x]+=b[i];
  63. }
  64.  
  65. int ans=;
  66. for (int i=;i<=t;i++)
  67. {
  68. for (int j=;j<=w;j++)
  69. {
  70. dp[i][j]=dp[i-][j];
  71. if (j>=s1[i]) dp[i][j]=max(dp[i][j],dp[i-][j-s1[i]]+s2[i]);
  72. for (int k=;k<g[i].size();k++)
  73. {
  74. if (j>=a[g[i][k]]) dp[i][j]=max(dp[i][j],dp[i-][j-a[g[i][k]]]+b[g[i][k]]);
  75. }
  76. if (i==t) ans=max(ans,dp[i][j]);
  77. }
  78. }
  79. printf("%d\n",ans);
  80.  
  81. return ;
  82. }

C:

题目大意:n对男女(2n个人)围成一圈,要给他们黑白染色,要求任意三个相邻的人颜色不能完全一样。  第i对情侣分别是ai和bi,他们的颜色要不一样。 n<=100000.

题解:

比赛的时候没有想出来...下面是官方题解:

首先给ai和bi连边,然后给2*i-1和2*i 连边,可以证明这个图是二分图,做一次染色就好啦。

证明:

记给ai和bi连的边为A类边,2*i-1和2*i 连的边为B类边。

由于一个人不可能和多个人是男女朋友关系,所以对于每个点有且只有1条A类边和它相连,同时有且只有1条B类边。

考虑任意一个环。 对于环上的边,只能是AB类边交替,否则就会有2条A类边或者2条B类边和同一个点相连。

因此环不可能是奇环。 故这个图是二分图。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <cstdlib>
  9. #include <set>
  10. #include <queue>
  11. using namespace std;
  12.  
  13. #define X first
  14. #define Y second
  15. #define Mod 1000000007
  16. #define N 200110
  17. #define M 200110
  18.  
  19. typedef long long ll;
  20. typedef pair<int,int> pii;
  21.  
  22. const ll INF=4e18;
  23.  
  24. int n;
  25. vector<int> g[N];
  26. int color[N],a[N],b[N];
  27.  
  28. bool Dfs(int x,int c)
  29. {
  30. color[x]=c;
  31. for (int i=;i<g[x].size();i++)
  32. {
  33. int y=g[x][i];
  34. if (!color[y] && !Dfs(y,-c)) return false;
  35. else if (color[y]==color[x]) return false;
  36. }
  37. return true;
  38. }
  39.  
  40. int main()
  41. {
  42. //freopen("in.in","r",stdin);
  43. //freopen("out.out","w",stdout);
  44.  
  45. scanf("%d",&n);
  46. for (int i=;i<=n;i++)
  47. {
  48. int x,y;
  49. scanf("%d%d",&x,&y);
  50. a[i]=x,b[i]=y;
  51. g[x].push_back(y);
  52. g[y].push_back(x);
  53. }
  54. for (int i=;i<=n;i++) g[*i-].push_back(*i),g[*i].push_back(*i-);
  55. for (int i=;i<=*n;i++) if (!color[i]) Dfs(i,);
  56. for (int i=;i<=n;i++) printf("%d %d\n",color[a[i]],color[b[i]]);
  57. return ;
  58. }

Codeforces Round #383 (Div. 1)的更多相关文章

  1. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  2. Codeforces Round #383 Div 1题解

    第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...

  3. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  4. Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包

    A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...

  5. dfs + 最小公倍数 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...

  6. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  7. Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  8. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  9. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  10. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或

    题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...

随机推荐

  1. 【2014-05-06】C++ 设计模式----单例模式

    1.何为单例模式? 单例模式(Singleton),保证一个类仅有一个实例,并提供一个访问它的全局访问点(static).可能有人会想这和全局变量有什么区别呢? 通常我们可以让一个全局成员变量使得一个 ...

  2. 由Excel表格导出Latex代码

    Latex提供了不少绘制表格的宏包(参见:http://tug.org/pracjourn/2007-1/mori/),但在latex里画表并不直观,特别是在表格比较大的时候,有时候也需要先用Exce ...

  3. An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

    如果你和我一样遇到了这个问题,那么你就要检查你要操作的Model对象查询,更新操作的数据库上下文也就是DBContext是否一致.如果不一致也就是说你用AContext去查如AContext.SET& ...

  4. iOS runloop初步学习

    参考: http://www.aichengxu.com/view/43297111. 定义:其实它内部就是do-while循环,在这个循环内部不断地处理各种任务(比如Source.Timer.Obs ...

  5. iOS中iconfont(图标字体)的基本使用

    前言 近日在做项目时,项目组有提出iconfont的技术,便开始查询相关资料.iconfont技术的主要目的是为减少应用体积而生.首先icon代表图标 font代表字体.此技术便是将图标转化为字体,从 ...

  6. Android文件存储

    文件存储是Android中最基本的一种数据存储方式,它不读存储的内容进行任何的格式化处理,所有数据原封不动的保存在文件之中.如果想用文件存储的方式保存一些较为复杂的数据,就需要定义一套自己的格式规范, ...

  7. unity调用摄像头的方法

    http://blog.csdn.net/cocoa_china/article/details/10527995 using UnityEngine; using System.Collection ...

  8. 7 -- Spring的基本用法 -- 4...

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  9. 学习笔记找到多个具有相同 ID“_header”的控件,FindControl 要求控件具有唯一的 ID.

    解决 找到多个具有相同 ID“_header”的控件,FindControl 要求控件具有唯一的 ID. private void DisplayHotBooks()    {        //获取 ...

  10. hihocoder挑战赛26

    某蒟蒻成功的·写出了T1并rank16...小岛的题目真难... 传送门:http://hihocoder.com/contest/challenge26 T1 如果你想要暴力枚举的话显然是不行的 如 ...