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 ...
随机推荐
- 解迷宫的C++的未完善编程代码........请大神们帮忙改善下.........
这...................................................................... 我也是醉了 看不太懂,大神们求解............ ...
- python两种生成md5的方法
一. 使用md5包 import md5 src = 'this is a md5 test.' m1 = md5.new() m1.update(src) print m1.hexdigest() ...
- yii2 renderPartial
在 views/news/_copyright.php 中插入以下代码: <div> This is text about copyright data for news items &l ...
- easyui combobox 左匹配模糊查询
之前一直不知道,easyui 的combobox还有从左匹配查询显示数据的. 样式是这样的:(这是数据是已经存在下拉列表里的) 在这样操作的时候,遇到了一个问题.(其实也不算问题的). 就是操作人员在 ...
- Mysql主从同步遇到的一些问题
为提供更快的访问速度,在不同的地区增加了一台只供访问的从服务器.因此要将主服务器的数据全部备份过去,并且设置主从同步 假设: 主服务器IP:192.168.1.10 从服务器IP:192.168.1. ...
- SQL SERVER 2008 评估期已过
开始-->运行-->regedit 修改注册表: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\Config ...
- Windows下 Maven 使用 阿里云镜像配置
新建或者修改文件: C:\Users\user\.m2\settings.xml <settings xmlns="http://maven.apache.org/SETTINGS/1 ...
- HTML5新增标签
section标签 <section>标签,定义文档中的节.比如章节.页眉.页脚或文档中的其它部分.一般用于成节的内容,会在文档流中开始一个新的节.它用来表现普通的文档内容或应用区块,通 ...
- Java中用ClassLoader载入各种资源(类、文件、web资源)的方法
lassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回这个类的class对象. ClassLoader负责载入系 ...
- 创建用资源管理器打开FTP位置
FTP快捷方式默认用浏览器打开,而不是用资源管理器打开,管理文件不习惯. 解决方法1:创建桌面快捷方式 新建快捷方式,键入对象位置 %windir%\explorer.exe "ftp:// ...