唉,昨天晚上迷迷糊糊地去打cf,结果fst两题,掉回蓝了...

A.Andryusha and Colored Balloons

题意:给定一棵树,任意两个距离小等于二的点不能染相同的颜色,求最小颜色数和染色方案。 n<=2*10^5

题解:求一个度数最大的点,度数+1即是颜色数,然后暴力染色。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
} struct edge{
int to,next;
}e[];
int head[];
int n,rt,cnt=;
bool b[];
int ans=;
int num[];
int col[];
int beg[]; void ins(int f,int t)
{
e[++cnt].next=head[f];head[f]=cnt;
e[cnt].to=t;
} void dfs(int x,int fa)
{
num[x]=;
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
{
dfs(e[i].to,x);
num[x]++;
}
if(fa!=)num[x]++;
} void solve(int x,int fa)
{
// cout<<"solve"<<x<<" "<<fa<<" "<<col[x]<<" "<<col[fa]<<endl;
int c=;
for(int i=head[x];i;i=e[i].next,c=c+)
if(!col[e[i].to])
{
while(c==col[x]||c==col[fa])c=c+;
col[e[i].to]=c;
solve(e[i].to,x);
}
else c--;
} int main()
{
n=read();
for(int i=;i<n;i++)
{int u=read(),v=read();ins(u,v);ins(v,u);}
dfs(,);for(int i=;i<=n;i++)if(num[i]>ans){ans=num[i];rt=i;}
col[rt]=;solve(rt,);printf("%d\n",ans);
for(int i=;i<=n;i++)printf("%d ",col[i]);
return ;
}

B.Innokenty and a Football League

题意:有n个足球队,每个队有两个选名字的方案,也就是直接给定了两个名字。所有球队不能有相同的名字。

特殊的,如果有两个球队第一个名字相同,一个球队选了第二个名字,另一个球队不能选第一个名字。n<=1000

题解:2-sat

据说有个学长188行大暴力过了,真的佩服。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
int n,dn=;
char ch[][];
char s[];
int h1[],h2[],q[];
bool inq[];
struct edge{
int to,next;
}e[];
int head[],dfn[],low[],bel[],col[],in[];
int cnt=,cc,top=,cft[];
queue<int> qu; void ins(int f,int t)
{
e[++cnt].next=head[f];head[f]=cnt;
e[cnt].to=t;
} void hashh(int k)
{
// cout<<k<<" "<<ch[k][1]<<" "<<ch[k][2]<<" "<<ch[k][3]<<" "<<ch[k][4]<<endl;
int hash1,hash2;
hash1=hash2=(ch[k][]-'A')*+ch[k][]-'A';
hash1=hash1*+(ch[k][]-'A');
hash2=hash2*+(ch[k][]-'A');
h1[k]=hash1;h2[k]=hash2;
} void tj(int x)
{
//cout<<"tj"<<x<<endl;
dfn[x]=low[x]=++dn;inq[x]=;q[++top]=x;
for(int i=head[x];i;i=e[i].next)
{
int v=e[i].to;
if(!dfn[v]){tj(v);low[x]=min(low[x],low[v]);}
else {if(inq[v])low[x]=min(low[x],dfn[v]);}
}
if(dfn[x]==low[x])
for(++cc;q[top+]!=x;bel[q[top]]=cc,inq[q[top]]=,top--);
} void rebuild()
{
for(int i=;i<=n<<;i++)
for(int j=head[i];j;j=e[j].next)
if(bel[i]!=bel[e[j].to])
ins(bel[i],bel[e[j].to]),in[e[j].to]++;
} void topsort()
{
for(int i=n*+;i<=cc;i++)
if(in[i]==) qu.push(i);
while(!qu.empty())
{
int u=qu.front();qu.pop();
if(!col[u])
for(int j=;j<=n<<;j++)
if(bel[j]==u)
col[bel[j>n?j-n:j+n]]=;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
--in[v];
if(!in[v]) qu.push(v);
}
}
} int main()
{
n=read();cc=n<<;
for(int i=;i<=n;i++)
{
scanf("%s",s+);
ch[i][]=s[];ch[i][]=s[];ch[i][]=s[];
scanf("%s",s+);ch[i][]=s[];
hashh(i);
}
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{
if(h1[i]==h1[j]&&h2[i]==h2[j]) return *puts("NO");
if(h1[i]==h1[j]){ins(i,j+n);ins(j,i+n);ins(i+n,j+n);ins(j+n,i+n);}
if(h1[i]==h2[j]){ins(i,j);ins(j+n,i+n);}
if(h2[i]==h1[j]){ins(i+n,j+n);ins(j,i);}
if(h2[i]==h2[j]){ins(i+n,j);ins(j+n,i);}
}
for(int i=;i<=n*;i++)if(!dfn[i])tj(i);
//for(int i=1;i<=n;i++)cout<<bel[i]<<" "<<bel[i+n]<<endl;
for(int i=;i<=n;i++)if(bel[i]==bel[i+n])return *puts("NO");
rebuild();
topsort();
puts("YES");
for(int i=;i<=n;i++)if(col[bel[i]]==)printf("%c%c%c\n",ch[i][],ch[i][],ch[i][]);
else printf("%c%c%c\n",ch[i][],ch[i][],ch[i][]);
return ;
}

C.Underground Lab

题意:给定一个n个点m条边的连通图,然后你要用k条路径覆盖所有的点,每条路径最多2*n/k向上取整个点,求一个方案。n<=2*10^5

题解:假装它是一棵树,然后求一个欧拉序列,只有2*n-1个点,然后分段输出就行了。

注意处理输出之后不足k组的情况,乱填充一下就行。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
} int ans[];
bool b[];
struct edge{
int to,next;
}e[];
int head[];
int n,m,k,cnt=,top=,q[]; void ins(int f,int t)
{
e[++cnt].next=head[f];head[f]=cnt;
e[cnt].to=t;
} void dfs(int x,int fa)
{
b[x]=;q[++top]=x;
for(int i=head[x];i;i=e[i].next)
if(!b[e[i].to])
{
dfs(e[i].to,x);
q[++top]=x;
}
} int main()
{
n=read();m=read();k=read();
for(int i=;i<=m;i++)
{int u=read(),v=read();ins(u,v);ins(v,u);}
dfs(,);
int l=(top+k-)/k;
int i=;
for(int pre=;i<=k&&pre<top;i++,pre+=l)
{
int l2=min(l,top-pre);
printf("%d ",l2);
for(int j=;j<=l2;j++)
printf("%d ",q[pre+j]);
puts("");
}
while(i<=k){i++;printf("1 1\n");}
return ;
}

D.Axel and Marston in Bitland

给丁一个n点m边的图,然后每条边有一个类别1或者0。

你要按照给定的顺序走边:一条0的,然后把刚走的取反接起来,然后一直做。比如P代表0,B代表1,那么你要这么走:

P, PB, PBBP, PBBPBPPB, PBBPBPPBBPPBPBBP, and so on.

求最多能走多少条。如果答案>10^18,输出-1   n<=500

题解:倍增,用bitset来求出长度为k,开头是0或者1,所有点对的连通性,然后dp

用f[k][i][0/1]表示开头是0/1,第i个点,最后一次走的长度是2^k的最大距离。

复杂度:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#include<bitset>
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
bitset<> s[][][];
ll f[][][];
int n,m;
int main()
{
n=read();m=read();
for(int i=;i<=m;i++)
{
int u=read(),v=read(),w=read();
s[w][u][][v]=;
}
for(int k=;k<;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(s[][i][k][j])s[][i][k+]|=s[][j][k];
if(s[][i][k][j])s[][i][k+]|=s[][j][k];
}
memset(f,,sizeof(f));f[][][]=;
for(int k=;k>=;k--)
{
for(int i=;i<=n;i++)
{
f[][i][k]=f[][i][k+];
f[][i][k]=f[][i][k+];
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(s[][i][k][j]) f[][j][k]=max(f[][j][k],f[][i][k+]+(1LL<<k));
if(s[][i][k][j]) f[][j][k]=max(f[][j][k],f[][i][k+]+(1LL<<k));
}
}
}
ll mx=;
for(int i=;i<=n;i++)mx=max(mx,max(f[][i][],f[][i][]));
if(mx>=1000000000000000000LL)puts("-1");
else printf("%I64d\n",mx);
return ;
}

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

  1. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  2. 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E

    http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...

  3. 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D

    http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...

  4. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab

    地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...

  5. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League

    地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...

  6. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons

    地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...

  7. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed

    地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks

    地址:http://codeforces.com/contest/782/problem/A 题目: A. Andryusha and Socks time limit per test 2 seco ...

  9. Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

    Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...

随机推荐

  1. 安装Loadrunner 11.0时,弹出缺少2.8 sp1组件--解决方案(win7)

    这是因为注册表缺少FullInstallVer和Version,归根到底是madc安装的的问题 以下是解决方法: 1.运行regedit,打开注册表,进入HKEY_LOCAL_MACHINE\SOFT ...

  2. div+css命名规则

    作为一个前端菜鸟,进公司的第一个项目就是中途从外包公司接过来的公司网站,在别人写过了的基础上接着写,命名什么的,简直不要太痛苦. 目前,这个网站已经完成,但是被后台人员指出命名不规范.有心想解释一两句 ...

  3. Python内置函数(61)——eval

    英文文档: eval(expression, globals=None, locals=None) The arguments are a string and optional globals an ...

  4. 快速搭建fabric-v1.1.0的chaincode开发环境

    本文参考了fabric官方文档:http://hyperledger-fabric.readthedocs.io/en/latest/peer-chaincode-devmode.html?highl ...

  5. spring MVC中定义异常页面

    如果我们在使用Spring MVC的过程中,想自定义异常页面的话,我们可以使用DispatcherServlet来指定异常页面,具体的做法很简单: 下面看我曾经的一个项目的spring配置文件: 1 ...

  6. jquery中attr与prop的区别

    先从一个老生常谈的问题说起,使用jquery实现全选全不选.楼主先使用的jquery版本是 jquery-1.11.1.min.js 全选<input type="checkbox&q ...

  7. RxJava系列番外篇:一个RxJava解决复杂业务逻辑的案例

    之前写过一系列RxJava的文章,也承诺过会尽快有RxJava2的介绍.无奈实际项目中还未真正的使用RxJava2,不敢妄动笔墨.所以这次还是给大家分享一个使用RxJava1解决问题的案例,希望对大家 ...

  8. POJ1236【Tarjan+缩点】

    题目大意:有向关系体现在电脑可以通过网络单向的传输文件,并规定一旦有电脑存在该文件,那么所有它能传输的电脑就能在第一时间得到这个文件,题目有两个问题,第一个是最少向网络中的几台电脑投放文件,能使得整个 ...

  9. POJ-2586 Y2K Accounting Bug贪心,区间盈利

    题目链接: https://vjudge.net/problem/POJ-2586 题目大意: MS公司(我猜是微软)遇到了千年虫的问题,导致数据大量数据丢失.比如财务报表.现在知道这个奇特的公司每个 ...

  10. hdu2050 折线分割平面---递推

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2050 题目大意: 求n条折线分割平面的最大数目 思路: 先看n条直线的时候 一条直线 2个平面 两条 ...