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)的更多相关文章

  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. 网络HTTP协议

    WebView:在应用中嵌入一个浏览器 ...... webView = (webView)findViewById(R.id.web_view); webView.getSettings().set ...

  2. Reactjs的Controller View模式

    摘要:做一个可以利用props来控制和传递所有状态给其子组件的顶级组件是一件非常酷的事情 不要和“MVC”混淆了,只有能够控制和传递所有的“state”的顶层组件,我们才叫它"view co ...

  3. SLAM前端技术选择思考

    以前是专门做室内定位技术研究的,先后学习和分析了多种基于电磁的室内定位技术,如WiFi指纹定位(先后出现过RSSI.CTF.CIR多种指纹特征).WiFi ToF定位.低功耗蓝牙BLE以及iBeaco ...

  4. nignx 负载均衡的几种算法介绍

    负载均衡,集群必须要掌握,下面介绍的负载均衡的几种算法.   1 .轮询,即所有的请求被一次分发的服务器上,每台服务器处理请求都相同,适合于计算机硬件相同.   2.加权轮询,高的服务器分发更多的请求 ...

  5. Adobe Photoshop CC (32/64位) 绿色精简版

    32位版下载地址:http://pan.baidu.com/share/link?uk=33907222&shareid=3828486959 64位版下载地址:http://pan.baid ...

  6. 16-网易-intership

    1.多选 //HTML <p>很长的一段文字,很长的一段文字,很长的一段文字,特别长的文字</p> //CSS p{ width:100px; white-space:nowr ...

  7. Ember.js 的视图层

    本指导会详尽阐述 Ember.js 视图层的细节.为想成为熟练 Ember 开发者准备,且包 含了对于入门 Ember 不必要的细节. Ember.js 有一套复杂的用于创建.管理并渲染连接到浏览器 ...

  8. hadoop启动是常见小问题

    1.先su进入root账户,然后 service iptables stop //关闭防火墙 start-all.sh //启动 2.启动是会显示,如果出错日志保存路径!!!基本所有问题都要去这些日志 ...

  9. C# 拆箱与装箱 要严格控制,数量多起来严重影响效率

    int i = 5; object o = i; int j = (int)o; IComparer x = 5; 1. o的对象必须为一个引用,而数字5不是,则发生了装箱: 运行时将在堆上创建一个包 ...

  10. java: Thread 和 runnable线程类

    java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...