Codeforces Round #383 (Div. 1)
A:
题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x。
题解:
首先每个点应该都在一个环上,否则无解。
对于大小为k的奇环上的点,满足要求的最小的t是k.
对于大小为k的偶环上的点,满足要求的最小的t是k/2.
对于每个环求最小公倍数即可。 数据范围很小,直接暴力求环就可以了。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cstdlib>
- #include <set>
- using namespace std;
- #define X first
- #define Y second
- #define Mod 1000000007
- #define N 110
- typedef long long ll;
- typedef pair<int,int> pii;
- int n;
- int a[N];
- bool vis[N];
- ll gcd(ll x,ll y)
- {
- ll tmp;
- while (y)
- {
- tmp=x%y;
- x=y;y=tmp;
- }
- return x;
- }
- ll lcm(ll x,ll y)
- {
- return x/gcd(x,y)*y;
- }
- int main()
- {
- //freopen("in.in","r",stdin);
- //freopen("out.out","w",stdout);
- scanf("%d",&n);
- for (int i=;i<=n;i++) scanf("%d",&a[i]);
- ll ans=;
- for (int i=;i<=n;i++)
- {
- int t=i,c=;
- memset(vis,,sizeof(vis));
- do
- {
- vis[t]=true;
- c++,t=a[t];
- }while(!vis[t]);
- if (t==i)
- {
- if (!(c&)) c>>=;
- ans=lcm(ans,c);
- }
- else
- {
- printf("-1\n");
- return ;
- }
- }
- printf("%d\n",ans);
- return ;
- }
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).
代码:
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cstdlib>
- #include <set>
- using namespace std;
- #define X first
- #define Y second
- #define Mod 1000000007
- #define N 1010
- #define M 10000010
- typedef long long ll;
- typedef pair<int,int> pii;
- int n,m,w;
- int a[N],b[N],father[N],id[N];
- int s1[N],s2[N];
- int dp[N][N];
- vector<int> g[N];
- int Find(int x)
- {
- if (father[x]==x) return x;
- father[x]=Find(father[x]);
- return father[x];
- }
- void Merge(int x,int y)
- {
- x=Find(x),y=Find(y);
- if (x==y) return ;
- father[x]=y;
- }
- int main()
- {
- //freopen("in.in","r",stdin);
- //freopen("out.out","w",stdout);
- scanf("%d%d%d",&n,&m,&w);
- for (int i=;i<=n;i++) scanf("%d",&a[i]);
- for (int i=;i<=n;i++) scanf("%d",&b[i]),father[i]=i;
- int x,y;
- for (int i=;i<=m;i++)
- {
- scanf("%d%d",&x,&y);
- Merge(x,y);
- }
- int t=;
- for (int i=;i<=n;i++) if (Find(i)==i) id[i]=++t;
- for (int i=;i<=n;i++)
- {
- int x=id[Find(i)];
- g[x].push_back(i);
- s1[x]+=a[i];
- s2[x]+=b[i];
- }
- int ans=;
- for (int i=;i<=t;i++)
- {
- for (int j=;j<=w;j++)
- {
- dp[i][j]=dp[i-][j];
- if (j>=s1[i]) dp[i][j]=max(dp[i][j],dp[i-][j-s1[i]]+s2[i]);
- for (int k=;k<g[i].size();k++)
- {
- if (j>=a[g[i][k]]) dp[i][j]=max(dp[i][j],dp[i-][j-a[g[i][k]]]+b[g[i][k]]);
- }
- if (i==t) ans=max(ans,dp[i][j]);
- }
- }
- printf("%d\n",ans);
- return ;
- }
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类边和同一个点相连。
因此环不可能是奇环。 故这个图是二分图。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cstdlib>
- #include <set>
- #include <queue>
- using namespace std;
- #define X first
- #define Y second
- #define Mod 1000000007
- #define N 200110
- #define M 200110
- typedef long long ll;
- typedef pair<int,int> pii;
- const ll INF=4e18;
- int n;
- vector<int> g[N];
- int color[N],a[N],b[N];
- bool Dfs(int x,int c)
- {
- color[x]=c;
- for (int i=;i<g[x].size();i++)
- {
- int y=g[x][i];
- if (!color[y] && !Dfs(y,-c)) return false;
- else if (color[y]==color[x]) return false;
- }
- return true;
- }
- int main()
- {
- //freopen("in.in","r",stdin);
- //freopen("out.out","w",stdout);
- scanf("%d",&n);
- for (int i=;i<=n;i++)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- a[i]=x,b[i]=y;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- for (int i=;i<=n;i++) g[*i-].push_back(*i),g[*i].push_back(*i-);
- for (int i=;i<=*n;i++) if (!color[i]) Dfs(i,);
- for (int i=;i<=n;i++) printf("%d %d\n",color[a[i]],color[b[i]]);
- return ;
- }
Codeforces Round #383 (Div. 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 题解 直接 ...
- Codeforces Round #383 Div 1题解
第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...
- 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 ...
- 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 ...
- dfs + 最小公倍数 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...
- 01背包dp+并查集 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...
- 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 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- 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 ...
- 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 ...
随机推荐
- 【2014-05-06】C++ 设计模式----单例模式
1.何为单例模式? 单例模式(Singleton),保证一个类仅有一个实例,并提供一个访问它的全局访问点(static).可能有人会想这和全局变量有什么区别呢? 通常我们可以让一个全局成员变量使得一个 ...
- 由Excel表格导出Latex代码
Latex提供了不少绘制表格的宏包(参见:http://tug.org/pracjourn/2007-1/mori/),但在latex里画表并不直观,特别是在表格比较大的时候,有时候也需要先用Exce ...
- An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
如果你和我一样遇到了这个问题,那么你就要检查你要操作的Model对象查询,更新操作的数据库上下文也就是DBContext是否一致.如果不一致也就是说你用AContext去查如AContext.SET& ...
- iOS runloop初步学习
参考: http://www.aichengxu.com/view/43297111. 定义:其实它内部就是do-while循环,在这个循环内部不断地处理各种任务(比如Source.Timer.Obs ...
- iOS中iconfont(图标字体)的基本使用
前言 近日在做项目时,项目组有提出iconfont的技术,便开始查询相关资料.iconfont技术的主要目的是为减少应用体积而生.首先icon代表图标 font代表字体.此技术便是将图标转化为字体,从 ...
- Android文件存储
文件存储是Android中最基本的一种数据存储方式,它不读存储的内容进行任何的格式化处理,所有数据原封不动的保存在文件之中.如果想用文件存储的方式保存一些较为复杂的数据,就需要定义一套自己的格式规范, ...
- unity调用摄像头的方法
http://blog.csdn.net/cocoa_china/article/details/10527995 using UnityEngine; using System.Collection ...
- 7 -- Spring的基本用法 -- 4...
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- 学习笔记找到多个具有相同 ID“_header”的控件,FindControl 要求控件具有唯一的 ID.
解决 找到多个具有相同 ID“_header”的控件,FindControl 要求控件具有唯一的 ID. private void DisplayHotBooks() { //获取 ...
- hihocoder挑战赛26
某蒟蒻成功的·写出了T1并rank16...小岛的题目真难... 传送门:http://hihocoder.com/contest/challenge26 T1 如果你想要暴力枚举的话显然是不行的 如 ...