唉,昨天晚上迷迷糊糊地去打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. nyoj 非洲小孩

    非洲小孩 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 家住非洲的小孩,都很黑.为什么呢?第一,他们地处热带,太阳辐射严重.第二,他们不经常洗澡.(常年缺水,怎么洗 ...

  2. Docker_部署jenkins(dockerfile实现)

    docker+jenkins开始合体! 我用的是ubuntu14.04的基础镜像,具体的这里不做赘述. 我在/tmp/目录下建了一个Dockerfile文件: touch Dockerfile vi ...

  3. 消除ExtJS6的extjs-trila字样

  4. $(function(){})和window.onload的区别

    (1)$(function(){}):DOM节点创建 完成才执行 (2)window.onload:页面所有资源(JS/CSS)加载完成才执行

  5. Mysql数据库mys和ora库的备份与恢复脚本

    !/bin/bash Time=$(date +%Y%md%H%M%S) Back_dir="$HOME/mysqlback/${Time}" function Detect_u_ ...

  6. LDAP是什么

    LDAP的英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.LDAP目录服务是一种特殊的数据库系统,其专门针对读取,浏览和搜索操作进行了特定的 ...

  7. NHibernate与IbatisNet的简单比较

    NHibernate是当前最流行的Java O/R mapping框架Hibernate的移植版本,当前版本是1.0 rc-1.它出身于sf.net..IbatisNet是另外一种优秀的Java O/ ...

  8. 转:NLP+句法结构(三)︱中文句法结构(CIPS2016、依存句法、文法)

    NLP+句法结构(三)︱中文句法结构(CIPS2016.依存句法.文法)转自:https://www.cnblogs.com/maohai/p/6453389.html 摘录自:CIPS2016 中文 ...

  9. linux ubunt 安装软件的前期准备——更新源的更换

    如果是高手,请翻到页面最下方,更换更新源的总结,直接操作即可 可能会优点啰嗦,但是认真看,一定能解决问题~~希望对大家有帮助~ 最近在熟悉linux环境,自己安装了一个ubuntu虚拟机. 很多朋友问 ...

  10. python API验证

    API验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 API验证:     a. 发令牌: 静态         PS: 隐患 key ...