模拟题 day2
出题人: liu_runda
题目名称 一盘大棋 下一代互联网 强连通分量
源程序文件名 chess.cpp net.cpp scc.cpp
输入文件名 chess.in net.in scc.in
输出文件名 chess.out net.out scc.out
每个测试点时限 1s 1s 1s
内存限制 512MB 512MB 512MB
测试点数目 10 10 10
每个测试点分值 10 10 10
是否打开O2 优化 是 是 是
在windows 下用lemon 进行测试.
一盘大棋(chess)
【题目描述】
给出一个R*C 的棋盘.共有R 行C 列,R*C 个格子.现要在每个格子都填一个非
负整数.使得任意一个2*2 的正方形区域都满足这样的性质:左上角的数字+右下
角的数字=左下角的数字+右上角的数字.有些格子已经确定,你不能更改其中的
数字.其他格子的数字由你决定.下面是一个符合要求的3*3 的棋盘:
1 2 3
2 3 4
4 5 6
不难验证每个2*2 的区域都是符合要求的.
liu_runda 准备下一盘大棋,为此,他想要知道一个可行的填充棋盘的方案.但是
这个方案可能很大.所以你只需对给定的棋盘判定是否存在至少一种可行的填充
棋盘的方案.
【输入格式】
第一行输入一个T,表示数据组数。接下来T 组数据。
每组数据的第1 行3 个整数R,C,n,R 和C 表示棋盘的大小. n 表示已经被填好
数字的格子的数目
接下来n 行每行3 个整数ri,ci,ai,表示第ri 行ci 列的格子被填上了数字ai.
【输出格式】
T 行.第i 行是第i 组数据的答案.有合法方案时输出一行Yes,没有时输出一行
No.
【样例输入】
6
2 2 3
1 1 0
1 2 10
2 1 20
2 3 5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
2 2 3
1 1 20
1 2 10
2 1 0
3 3 4
1 1 0
1 3 10
3 1 10
3 3 20
2 2 4
1 1 0
1 2 10
2 1 30
2 2 20
1 1 1
1 1 -1
【样例输出】
Yes
No
No
Yes
No
No
【数据范围】
第1 个测试点,R=1
第2,3 个测试点,R*C<=12,如果有解,保证存在一个解使得所有数字大小不超过2
第4,5 个测试点,R=2
第6,7 个测试点,R=3
第8 个测试点,1<=R,C<=20
第9 个测试点,1<=R,C<=100
对于全部测试点,1<=T<=6,1<=R,C,n<=100000,1<=ri<=R,1<=ci<=C,同一个格子不
会多次被填上数字.ai 是整数且绝对值不超过10^9.
下一代互联网(net)
【题目描述】
下一代互联网,基于IPv6 架构,很有可能改变互联网世界.然而这个题和IPv6 并没有
什么关系.起成这个名字,是因为liu_runda 在铺网线的时候希望下一代互联网早点出现.
现在给出一个a*b 的金属网格.你有两种网线:长度为1 的,和长度为根号2 的.长度为
1 的网线只能让你连接一个交叉点和它上下左右方向的交叉点,长度为根号2 的网线只
能让你连接一个交叉点和它斜对角方向的交叉点.
在这个金属网格上,有n 个坐标放置了路由器.现在希望用网线把这n 个路由器连接
起来.具体来说,你可以从一个路由器出发,连接若干条首尾相接的长度为1 或根号2 的网
线到达另一个路由器,那么这两个路由器就被连通了.中间不能分叉,必须一直到达另一个
路由器.从一个路由器可以引出多根网线.我们希望最终任意两个路由器直接或者间接连
通.
我们希望铺设网络消耗的网线尽量少.liu_runda 首先发现,要连接两个横纵坐标都不
同的路由器,先尽量使用长度为根号2 的网线,可以减少使用网线的根数和总长度,所以他
规定任意一对直接连接的路由器,在连接这一对路由器使用网线长度最小的前提下,都必
须尽可能使用长度为根号2 的网线.
接下来liu_runda 发现,长度为根号2 的网线比长度为1 的网线贵得多…但是liu_runda
并不想收回他的规定.现在,他只能在已经有的辣鸡规定下,合理地选择哪几对路由器进行
连接,尽量减少长度根号2 的网线的使用数目.(即使这样的方案下网线的总长度不一定最
短)请问他最少需要使用多少根长度为根号2 的网线?
【输入格式】
第一行三个数字a,b,n 分别表示行数(路由器的纵坐标范围),列数(路由器的横坐标范
围)和路由器的数目.
接下来n 行,第i 行两个数字x[i],y[i]依次表示第i 个路由器的横坐标和纵坐标
【输出格式】
一行一个整数,表示liu_runda 至少需要使用的长度为根号2 的网线数目.
【样例输入】
【样例输出】
【数据范围】
第1 个测试点,n=2
第2 个测试点,n=3
第3,4,5,6 个测试点,n<=500,0<a,b <=105
所有数据,n<=105,0<a,b<=109,0<y[i]<=a,0<x[i]<=b.不会出现横纵坐标相同的两个路由
器.
强连通分量(scc)
【题目描述】
给出一张有向图,统计图中有多少个强联通分量,并从大到小依次输出每个强连通分
量的大小.
【输入格式】
第一行两个整数n,m 表示点数和边数
接下来m 行每行两个数字u,v 表示从u 到v 有一条有向边.
第一行一个整数c 表示强连通分量的个数
第二行c 个整数,空格隔开,从大到小依次表示每个强连通分量包含的点数.
【样例输入】
3 3
1 2
2 1
1 3
【样例输出】
2
2 1
【数据范围】
第1,2,3 个测试点,所有强连通分量大小不超过2
第4,5,6 个测试点,n<=10,m<=20
不会有自环,但可能有重复的边.
所有的测试数据,n<=105,m<=5*105

 #include<cstdio>
#include<cassert>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
typedef long long ll;
int ufs[maxn];ll w1[maxn];
int R,C;
struct node{
int x,y,val;
void read(){
scanf("%d%d%d",&x,&y,&val);
assert(<=x&&x<=R&&<=y&&y<=C);
}
}P[maxn];
bool cmpx(const node &A,const node &B){
return A.x<B.x;
}
bool cmpy(const node &A,const node &B){
return A.y<B.y;
}
int find(int x){
if(x==ufs[x])return x;
int rt=find(ufs[x]);
w1[x]+=w1[ufs[x]];
return ufs[x]=rt;
}
bool link(int a,int b,ll w){
if(find(a)!=find(b)){
int ra=find(a),rb=find(b);
ufs[ra]=ufs[rb];
w1[ra]=w+w1[b]-w1[a];
return true;
}else{
return w1[a]==w+w1[b];
}
}
ll Min1[maxn],Min2[maxn];
int main(){
freopen("chess.in","r",stdin);
freopen("chess.out","w",stdout);
int tests;scanf("%d",&tests);
while(tests--){
bool flag=true;
scanf("%d%d",&R,&C);
for(int i=;i<=R;++i){
ufs[i]=i;w1[i]=;
}
int n;scanf("%d",&n);
for(int i=;i<=n;++i)P[i].read();
for(int i=;i<=n;++i)if(P[i].val<)flag=false; sort(P+,P+n+,cmpy);
for(int i=;i<n;++i)
if(P[i].y==P[i+].y)
if(!link(P[i].x,P[i+].x,P[i+].val-P[i].val))flag=false; memset(Min1,0x3f,sizeof(Min1));
memset(Min2,0x3f,sizeof(Min2));
for(int i=;i<=n;++i){
int rt=find(P[i].x);
Min1[rt]=min(Min1[rt],P[i].val+w1[P[i].x]);
}
for(int i=;i<=R;++i){
int rt=find(i);
Min2[rt]=min(Min2[rt],-w1[i]);
}
for(int i=;i<=R;++i)
if(ufs[i]==i&&Min1[i]+Min2[i]<)flag=false;
printf("%s\n",flag?"Yes":"No");
}
return ;
}

chess.cpp

 #include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int ufs[maxn];
int find(int x){
return (x==ufs[x])?ufs[x]:(ufs[x]=find(ufs[x]));
}
struct point{
int x,y,num;
}P[maxn];
bool cmpx(const point &A,const point &B){
return A.x<B.x;
}
bool cmpy(const point &A,const point &B){
return A.y<B.y;
}
struct edge{
int u,v,w;
edge(int a=,int b=,int c=){
u=a;v=b;w=c;
}
bool operator <(const edge &B)const{
return w<B.w;
}
}E[maxn*];int tot=;
int main(){
freopen("net.in","r",stdin);
freopen("net.out","w",stdout);
int n;
scanf("%*d%*d%d",&n);
for(int i=;i<=n;++i){
scanf("%d%d",&P[i].x,&P[i].y);
P[i].num=i;
}
sort(P+,P+n+,cmpx);
for(int i=;i<n;++i){
E[++tot]=edge(P[i].num,P[i+].num,P[i+].x-P[i].x);
}
sort(P+,P+n+,cmpy);
for(int i=;i<n;++i){
E[++tot]=edge(P[i].num,P[i+].num,P[i+].y-P[i].y);
}
sort(E+,E+tot+);
for(int i=;i<=n;++i)ufs[i]=i;
int ans=;
for(int i=;i<=tot;++i){
if(find(E[i].u)!=find(E[i].v))ufs[find(E[i].u)]=find(E[i].v),ans+=E[i].w;
}
printf("%d\n",ans);
return ;
}

net.cpp

 #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
vector<int> to[maxn];
int n,m;
int dfn[maxn],low[maxn],T;
int stk[maxn],top;bool ins[maxn];
int sz[maxn],tot;
void dfs(int x){
dfn[x]=low[x]=++T;
ins[x]=true;stk[++top]=x;
for(vector<int>::iterator pt=to[x].begin();pt!=to[x].end();++pt){
if(!dfn[*pt]){
dfs(*pt);
if(low[*pt]<low[x])low[x]=low[*pt];
}else{
if(ins[*pt]&&dfn[*pt]<low[x])low[x]=dfn[*pt];
}
}
if(low[x]==dfn[x]){
++tot;
do{
//printf("%d ",stk[top]);
sz[tot]++;ins[stk[top]]=false;--top;
}while(stk[top+]!=x);
//printf("\n");
}
}
int main(){
freopen("scc.in","r",stdin);
freopen("scc.out","w",stdout);
scanf("%d%d",&n,&m);
int u,v;
for(int i=;i<=m;++i){
scanf("%d%d",&u,&v);
to[u].push_back(v);
}
for(int i=;i<=n;++i)if(!dfn[i])dfs(i);
sort(sz+,sz+tot+);
printf("%d\n",tot);
for(int i=tot;i>=;--i)printf("%d%c",sz[i],(i==)?'\n':' ');
return ;
}

scc.cpp

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<stack>
#define R(a,b,c) for(register int (a)=(b);(a)<=(c);++(a))
#define nR(a,b,c) for(register int (a)=(b);(a)>=(c);--(a))
#define Ii inline int
#define Il inline long long
#define Iv inline void
#define Id inline double
#define ll long long
#define re register
#define Cmax(a,b) ((a)=(a)>(b)?(a):(b))
#define Cmin(a,b) ((a)=(a)<(b)?(a):(b))
#define D_e(x) printf("&_____%d______&",x);
#define D_e_Line printf("\n-----------------\n");
#define Pause system(pause)
using namespace std;
const int N=;
const int M=;
Ii read(){
int s=,f=;char c;
for(c=getchar();c<''||c>'';c=getchar())if(c=='-')f=-;
while(c>=''&&c<='')s=s*+(c^''),c=getchar();
return s*f;
}
Iv print(int x){
if(x<)putchar('-'),x=-x;
if(x>)print(x/);
putchar(x%^'');
}
struct node{
int nxt,pre;
}e[M];
int id,color,head[M],cnt,dfn[N],low[N],vis[N],col[N],siz[N];
stack<int>sta;
Iv add(int u,int v){
e[++cnt]=(node){head[u],v},head[u]=cnt;
}
Iv Tarjan(int u){
dfn[u]=low[u]=++id;
sta.push(u),vis[u]=;
for(re int i=head[u];i;i=e[i].nxt){
int v=e[i].pre;
if(!dfn[v])
Tarjan(v),Cmin(low[u],low[v]);
else if(vis[v])
Cmin(low[u],dfn[v]);///////
}
if(low[u]==dfn[u]){
++color;
int now=-;
while(now!=u){
now=sta.top(),sta.pop(),
col[now]=color,
++siz[color],
vis[now]=;
}
}
}
//#define DEBUG ;
int main(){
freopen("scc.in","r",stdin),freopen("scc.out","w",stdout);
int n=read(),m=read();
R(i,,m){
int u=read(),v=read();
//for(re int i=head[u];i;i=e[i].nxt)
// if(e[i].pre==v)
// continue;
add(u,v);
}
R(i,,n)
if(!dfn[i])
Tarjan(i);
sort(siz+,siz+color+);
print(color),putchar('\n');
nR(i,color,)
printf("%d ",siz[i]);
#ifdef DEBUG
putchar('\n');
R(i,,n)
printf("%d-> %d\n",i,col[i]);
#endif
return ;
}
/*
6 6
1 4
1 2
2 5
1 5
1 3
3 6 6 6
1 4
1 2
2 5
5 1
1 3
3 6 3 4
1 2
1 2
2 3
3 1 5 5
1 2
2 3
3 1
1 5
4 1 3 3
1 2
2 3
3 1
*/

scc_MY.cpp

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<cmath>
//#include<stack>
#define R(a,b,c) for(register int (a)=(b);(a)<=(c);++(a))
#define nR(a,b,c) for(register int (a)=(b);(a)>=(c);--(a))
#define Ii inline int
#define Il inline long long
#define Iv inline void
#define Id inline double
#define Ib inline bool
#define ll long long
#define re register
#define Fill(a,b) memset((a),(b),sizeof((a)))
#define Cmax(a,b) ((a)=(a)>(b)?(a):(b))
#define Cmin(a,b) ((a)=(a)<(b)?(a):(b))
#define D_e(x) printf("&_____%d______&",x);
#define D_e_Line printf("\n-----------------\n");
#define aause system("pause")
using namespace std;
const int N=;
//const int M=500001;
Ii read(){
int s=,f=;char c;
for(c=getchar();c<''||c>'';c=getchar())if(c=='-')f=-;
while(c>=''&&c<='')s=s*+(c^''),c=getchar();
return s*f;
}
Iv print(int x){
if(x<)putchar('-'),x=-x;
if(x>)print(x/);
putchar(x%^'');
}
int fa[N];
int Find(int x){
return x==fa[x]?fa[x]:fa[x]=Find(fa[x]);
}
struct node{
int x,y,id;
}a[N];
bool cmp_x(const node &a,const node &b){
return a.x<b.x;
}
bool cmp_y(const node &a,const node &b){
return a.y<b.y;
}
struct edge{
int l,r,w;
bool operator <(const edge &b)const{
return w<b.w;
}
}e[N<<];int cnt;
int main(){
freopen("net.in","r",stdin),freopen("net.out","w",stdout);
read(),read();
int n=read();
R(i,,n)
a[i]=(node){read(),read(),i},
fa[i]=i;
sort(a+,a+n+,cmp_x);
R(i,,n-)
e[++cnt]=(edge){a[i].id,a[i+].id,a[i+].x-a[i].x};
sort(a+,a+n+,cmp_y);
R(i,,n-)
e[++cnt]=(edge){a[i].id,a[i+].id,a[i+].y-a[i].y};
sort(e+,e+cnt+);
int ans=;
R(i,,cnt){
int p=Find(e[i].l),q=Find(e[i].r);
if(p!=q)
fa[p]=q,
ans+=e[i].w;
}
print(ans);
return ;
}

net_MY.cpp

CDQZ Day2的更多相关文章

  1. CDQZ集训DAY2 日记

    依然很爆炸. T1上来有50分暴力分,打完后注意到了后50分的随机数据,开始想怎么去对付他.然后就开始思考随机数据意味着什么.想了想,想打一个扫描线或者分治.决策了一下还是打了一个扫描线+链表.然而只 ...

  2. CDQZ多校集训记

    20171218 DAY0 初相逢 今天的阳光很好,确实好极了.下午开始时,mercer说门外站了一堆人,我看都不用看就知道是衡水的.衡水人,怎么说呢,觉得还是挺不一样的.不知道像凡哥和超哥这种奇异的 ...

  3. 【从零开始学BPM,Day2】默认表单开发

    [课程主题]主题:5天,一起从零开始学习BPM[课程形式]1.为期5天的短任务学习2.每天观看一个视频,视频学习时间自由安排. [第二天课程] Step 1 软件下载:H3 BPM10.0全开放免费下 ...

  4. 记一周cdqz训练

    #include <cstdio> using namespace std; int main(){ puts("转载请注明出处:http://www.cnblogs.com/w ...

  5. NOIp2016 Day1&Day2 解题报告

    Day1 T1 toy 本题考查你会不会编程. //toy //by Cydiater //2016.11.19 #include <iostream> #include <cstd ...

  6. day2

    三级菜单: ))))))))))] last_levels.pop() ]]]]]]]]:] information = : ch = msvcrt.getch() ][][: : password= ...

  7. java day2一个模拟双色球的代码

    package day2; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt ...

  8. Python基础-day2

    1.Python模块python 中导入模块使用import语法格式:import module_name示例1: 导入os模块system('dir')列出当前目录下的所有文件 # _*_ codi ...

  9. 【BZOJ 4517】【SDOI 2016 Round1 Day2 T2】排列计数

    本蒟蒻第一次没看题解A的题竟然是省选$Round1$ $Day2$ $T2$ 这道组合数学题. 考试时一开始以为是莫队,后来想到自己不会组合数的一些公式,便弃疗了去做第三题,,, 做完第三题后再回来看 ...

随机推荐

  1. Partial Functions(偏函数)

    如果你想定义一个函数,而让它只接受和处理其参数定义域范围内的子集,对于这个参数范围外的参数则抛出异常,这样的函数就是偏函数(顾名思异就是这个函数只处理传入来的部分参数). 偏函数是个特质其的类型为Pa ...

  2. SecureCRT 下MySQL中文乱码问题终极解决方案-乾颐堂

    一.查看Linux主机系统字符集 命令: echo $LANG [root@pythontab.com ~]# echo $LANG [root@pythontab.com ~]# en_US.UTF ...

  3. smarty 使用php函数

    strtotime() time() <{if strtotime($activity.start_time) gt time()}> <a href="?action=d ...

  4. 【转】ACM各种WA的说明及可能的原因

    转载地址:http://blog.csdn.net/qq_15015129/article/details/52738184 1.答案错误 —— wrong answer 就是最常见的.这个没办法,基 ...

  5. Hadoop压缩之CompressionCodecFactory

    1.CompressionCodecFactory简介 当在读取一个压缩文件的时候,可能并不知道压缩文件用的是哪种压缩算法,那么无法完成解压任务.在Hadoop中,CompressionCodecFa ...

  6. Word2013写CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  7. 编写高质量代码改善C#程序的157个建议——建议117:使用SSL确保通信中的数据安全

    建议117:使用SSL确保通信中的数据安全 SSL(Secure Socket Layer)最初是由NetScape公司设计的,用于Web安全的网络协议.目前它已经广泛应用到各类网络传输通信中了.SS ...

  8. 编写高质量代码改善C#程序的157个建议——建议113:声明变量前考虑最大值

    建议113:声明变量前考虑最大值 假设正在开发一个工资系统,其中一个模块负责处理加薪.代码如下: static void Main(string[] args) { ; salary = (); Co ...

  9. 洛谷P4234 最小差值生成树(lct动态维护最小生成树)

    题目描述 给定一个标号为从 11 到 nn 的.有 mm 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入输出格式 输入格式:   第一行两个数 n, mn,m ,表示图的点和边的数量. ...

  10. 推荐一款优秀的代码编辑软件--Source Insight

    “工欲善其事,必先利其器”,好的工具能够提高我们办事的效率,进而提升团队乃至企业的竞争力. 对于软件开发工程师来说,选择一款优秀的代码编辑软件也是很重要的.最近,我看到有很多同事还在用微软的VC ++ ...