代码:
#include <bits/stdc++.h>
#include "My_Random.h"
//#define random(x,y) (rand()%((y)-(x)+1)+(x))
//using namespace std;
using zdw::random;
//调用random(x,y)时请保证y>x,其余参数视为未定义行为
//传入的参数中不能含有负数
//返回值默认为unsigned long long(无符号64位整数)
#define ll long long
#define ull unsigned long long
#define errmsg(...) fprintf(stderr,__VA_ARGS__)
//A+B
namespace Solve_A{
int a,b;
int main(){
while(std::cin>>a>>b)
std::cout<<a+b<<std::endl;
return 0;
}
};
//随机输出若干行,每行两个数
namespace Data_A{
int main(){
int T=random(1,100),x,y;
while(T--){
x=random(1,2333);
y=random(1,2333);
printf("%d %d\n",x,y);
}
return 0;
}
};
//扫描联通块数量
namespace Solve_B{
std::vector<int>G[10005];
int C[10005];
int Q[10005],l,r;
void bfs(int s,int c){
C[Q[l=r=0]=s]=c;
while(l<=r){
int u=Q[l++];
for(auto &v:G[u])
if(!C[v])C[Q[++r]=v]=c;
}
}
int main(){
int n,m,ans=0;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&l,&r);
G[l].push_back(r);
G[r].push_back(l);
}
for(int i=1;i<=n;++i)
if(!C[i]){
bfs(i,i);
++ans;
}
printf("%d\n",ans);
return 0;
}
};
//生成一张随机图
namespace Data_B{
int main(){
int n=random(5000,10000);
int m=random(n,n*5);
int blocks=random(1,n/10);//将图分为blocks块(下限)
int tsz=n/blocks;//每个块的大小上限
int tm=m/blocks;//每个块的边数上限
int nowv=1;//当前点区间的左端点
printf("%d %d\n",n,m);
while(blocks-->1){//输出并留1个用于处理剩余
int sz=random(tsz/2,tsz);//当前块大小
int vv=nowv+sz-1;//当前块右端点
//ppints in range[nowv,vv]
int mm=random(tm/3,tm);//当前块边数
m-=mm;//总边数计数
//add edge to [nowv,vv]
while(mm--){
int x=random(nowv,vv);
int y=random(nowv,vv);
//在给定点区间上随机生成一条边
printf("%d %d\n",x,y);
}
nowv=vv+1;
}
//剩余的点和边
while(m--){
int x=random(nowv,n);
int y=random(nowv,n);
printf("%d %d\n",x,y);
}
return 0;
}
};
int main(){
zdw::set_rand_seed(time(0)+2333);
zdw::set_rand_seed2(time(0)+233);
/*
//test random
int x=random(1,100);
errmsg("%d\n",x);
ull z=random((ull)1e13,(ull)1e18);
errmsg("%llu\n",z);
//test over
*/
//A
errmsg("making A's data...\n");
freopen("A.in","w",stdout);
Data_A::main();
fclose(stdin);
//输入数据生成完毕
freopen("A.in","r",stdin);
freopen("A.out","w",stdout);
Solve_A::main();
fclose(stdin); fclose(stdout);
errmsg("A done...\n");
//输出数据生成完毕
//B
errmsg("making B's data...\n");
freopen("B.in","w",stdout);
Data_B::main();
fclose(stdin);
//输入数据生成完毕
freopen("B.in","r",stdin);
freopen("B.out","w",stdout);
Solve_B::main();
fclose(stdin); fclose(stdout);
errmsg("B done...\n");
//输出数据生成完毕
return 0;
}
#ifndef MY_RANDOM_H
#define MY_RANDOM_H
namespace zdw{
/******************************************
Don't use this(__random) namespace.
******************************************/
namespace __random{
const unsigned a=1664525;
const unsigned c=1013904223;
unsigned random_seed=2333,random_seed2=23333;
unsigned Random(unsigned &x){return x=a*x+c;}
unsigned Random(){return Random(random_seed);}
unsigned Random2(){return Random(random_seed2);}
};
void set_rand_seed(const unsigned &x){__random::random_seed=x;}
void set_rand_seed2(const unsigned &x){__random::random_seed2=x;}
unsigned random(){return __random::Random();}
//unsigned random(const unsigned &l,const unsigned &r){return __random::Random()%(r-l+1)+l;}
//unsigned random(const int &l,const int &r){return random((unsigned)l,(unsigned)r;}
unsigned long long random(const unsigned long long &l,const unsigned long long &r){return ((unsigned long long)__random::Random()<<32|(unsigned long long)__random::Random2())%(r-l+1)+l;}
};
#endif
和调用的头文件一样