#include<bits/stdc++.h>
#include<algorithm>
namespace gen{
using std::vector;
using std::stringstream;
using std::swap;
using std::shuffle;
using std::string;
using std::ostream;
using std::pair;
using std::set;
using std::uniform_int_distribution;
using std::uniform_real_distribution;
class Graph;
std::mt19937_64 randomer(time(0)^23232);
long long rnd(long long l,long long r){
uniform_int_distribution<long long>w(l,r);
return w(randomer);
}
Graph gen_chain(int point_count,bool directed,pair<long long,long long>weight_limit);
Graph gen_flower(int point_count,bool directed,pair<long long,long long>weight_limit);
Graph gen_tree(int point_count,bool directed,double chain,double flower,pair<long long,long long>weight_limit);
Graph gen_binary_tree(int point_count,double left,double right,pair<long long,long long>weight_limit);
Graph gen_graph(int point_count,int edge_count,bool self_loop,bool repeated_edges,bool directed,pair<long long,long long>weight_limit);
Graph gen_DAG(int point_count,int edge_count,bool self_loop,bool repeated_edges,bool loop,pair<long long,long long>weight_limit);
Graph gen_UDAG(int point_count,int edge_count,bool self_loop,bool repeated_edges,bool loop,pair<long long,long long>weight_limit);
Graph gen_hack_spfa(int point_count,int edge_count,bool directed,pair<long long,long long>weight_limit,int extra_edge);
class Edge{
public:
int start,end;
long long weight;
edge(int u,int v,long long w=1){
/*
int u -> the start vertex
int v -> the end vertex
int w -> the weight.
*/
this->start=u;
this->end=v;
this->weight=w;
return 1;//?
}
string to_string(){
/*
Return a string. The string contains the start vertex, end vertex and weight(u,v,w) and splits with space.
*/
stringstream x;
x<<start<<' '<<end<<' '<<weight;
return x.str();
}
string unweighted_edge(){
/*
Return a string. The string contains the start vertex, end vertex(u,v) and splits with space.
*/
stringstream x;
x<<start<<' '<<end;
return x.str();
}
};
class Graph{
bool directed;
vector<vector<Edge>>edges;
public:
Graph(int point_count,bool directed=0){
/*
int point_count -> the count of the vertexes in the graph.
bool directed = False -> whether the graph is directed(true:directed,false:not directed)
*/
this->directed=directed;
edges.resize(point_count+1);
}
vector<Edge> iterate_edges(){
/*
Iter the graph. Order by the start vertex.
*/
vector<Edge>res;
for(auto &node:edges){
for(auto &edge:node){
if(edge.end>=edge.start||this->directed){
res.push_back(edge);
}
}
}
return res;
}
string to_str(bool isshuffle=false,bool withweight=false){
/*
Convert the graph to string with format. Splits with "\n"
*/
string buf="";
vector<Edge> m=this->iterate_edges();
if(isshuffle)shuffle(m.begin(),m.end(),randomer);
for(auto &edge:m){
if(isshuffle&&!this->directed&&randomer()%2){
swap(edge.start,edge.end);
}
if(withweight){
buf+=edge.to_string();
}else{
buf+=edge.unweighted_edge();
}
buf+="\n";
}
return buf;
}
friend ostream& operator<<(ostream& out,Graph &print_graph){
return out<<print_graph.to_str();
}
void add_edge(int x,int y,int weight=1){
/*
int x -> the start vertex
int y -> the end vertex
int weight = 1 -> the weight
*/
edges[x].push_back({x,y,weight});
if(!directed&&x!=y){
edges[y].push_back({y,x,weight});
}
}
void add_edge(Edge w){
/*
int x -> the start vertex
int y -> the end vertex
int weight = 1 -> the weight
*/
edges[w.start].push_back(w);
if(!directed&&w.start!=w.end){
edges[w.end].push_back({w.end,w.start,w.weight});
}
}
Graph shuffled(){
auto old_edges=this->iterate_edges();
vector<int>transmit,transmit2;
for(auto &edge:old_edges){
transmit.push_back(edge.start);
transmit.push_back(edge.end);
}
sort(transmit.begin(),transmit.end());
auto enc=unique(transmit.begin(),transmit.end());
while(transmit.end()!=enc)transmit.pop_back();
transmit2=transmit;
shuffle(transmit2.begin(),transmit2.end(),randomer);
std::map<int,int>conn;
for(int i=0;i<transmit.size();i++)conn[transmit[i]]=transmit2[i];
auto graph=Graph(edges.size(),directed);
for(auto &edge:old_edges){
graph.add_edge(conn[edge.start],conn[edge.end],edge.weight);
}
return graph;
}
~Graph(){
edges.clear();
}
};
Graph gen_chain(int point_count,bool directed=false,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
bool directed = True -> whether the chain is directed(true:directed,false:not directed)
(int,int) weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
int weight_limit -> If you use a int for this arg, it means the max limit of the weight(included)
*/
return gen_tree(point_count,directed,1,0,weight_limit);
}
Graph gen_flower(int point_count,bool directed=false,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
bool directed = True -> whether the chain is directed(true:directed,false:not directed)
(int,int) weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
int weight_limit -> If you use a int for this arg, it means the max limit of the weight(included)
*/
return gen_tree(point_count,directed, 0, 1,weight_limit);
}
Graph gen_tree(int point_count,bool directed=false,double chain=0,double flower=0,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
double chain = 0 -> 1 means the tree is a chain
double flower = 0 -> 1 means the tree is a flower
NOTICE:only either chain or flower can be True
bool directed = False -> whether the chain is directed(true:directed,false:not directed)
pair<int,int> weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
*/
int ll=weight_limit.first,rr=weight_limit.second;
if(ll>rr)rr=ll;
if(chain<0||chain>1||flower<0||flower>1){
throw "chain and flower must be between 0 and 1";
exit(3);
}
if(chain+flower>1){
throw "chain plus flower must be smaller than 1";
exit(3);
}
auto graph=Graph(point_count, directed);
int chain_count = int((point_count - 1) * chain);
int flower_count = int((point_count - 1) * flower);
if(chain_count > point_count - 1){
chain_count = point_count - 1;
}
if(chain_count + flower_count > point_count - 1){
flower_count = point_count - 1 - chain_count;
}
int random_count = point_count - 1 - chain_count - flower_count;
for(int i=2;i<chain_count + 2;i++){
graph.add_edge(i-1,i,rnd(ll,rr));
}
for(int i=chain_count + 2;i<chain_count + flower_count + 2;i++){
graph.add_edge(1,i,rnd(ll,rr));
}
for(int i=point_count - random_count + 1;i<point_count + 1;i++){
graph.add_edge(rnd(1,i-1), i, rnd(ll,rr));
}
return graph;
}
Graph gen_binary_tree(int point_count,double left=0.0,double right=0.0,bool directed=0,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
double left = 0 -> random arg. should be in [0,1]
double right = 0 -> random arg. should be in [0,1]
NOTICE:left+right mustn't be greater than 1
bool directed = False -> whether the binary tree is directed(true:directed,false:not directed)
(int,int) weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
*/
int ll=weight_limit.first,rr=weight_limit.second;
if(ll>rr)rr=ll;
if(left<0||left>1||right<0||right>1){
throw "left and right must be between 0 and 1";
exit(3);
}
if(left+right>1){
throw "left plus right must be smaller than 1";
exit(3);
}
vector<int>can_left={1},can_right={1};
auto graph = Graph(point_count, directed);
for(int i=2;i<point_count + 1;i++){
uniform_real_distribution<double>x(0.0,1.0);
int node=0;
double edge_pos=x(randomer);
if(edge_pos < left ||(left + right < edge_pos && edge_pos <= (1.0 - left - right) / 2)){
int point_index=rnd(0,can_left.size()-1);
node=can_left[point_index];
int del_last_node=*can_left.rbegin();can_left.pop_back();
if(point_index < can_left.size()){
can_left[point_index] = del_last_node;
}
}else{
int point_index=rnd(0,can_right.size()-1);
node=can_right[point_index];
int del_last_node=*can_right.rbegin();can_right.pop_back();
if(point_index < can_right.size()){
can_right[point_index] = del_last_node;
}
}
can_left.push_back(i);
can_right.push_back(i);
}
return graph;
}
Graph gen_graph(int point_count,int edge_count,bool self_loop=false,bool repeated_edges=false,bool directed=false,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
int edge_count -> the count of edges
bool self_loop = Flase -> whether to allow self loops or not
bool repeated_edges = False -> whether to allow repeated edges or not
bool directed = False -> whether the chain is directed(true:directed,false:not directed)
pair<int,int> weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
*/
int ll=weight_limit.first,rr=weight_limit.second;
if(ll>rr)rr=ll;
auto graph = Graph(point_count, directed);
set<pair<int,int>>used_edges;
int i=0;
while(i<edge_count){
int u=rnd(1,point_count),v=rnd(1,point_count);
if(!self_loop&&u==v)continue;
if(!repeated_edges&&used_edges.find({u,v})!=used_edges.end())continue;
graph.add_edge(u,v,rnd(ll,rr));
if(!repeated_edges){
used_edges.insert({u,v});
if(!directed){
used_edges.insert({v,u});
}
}
i++;
}
return graph;
}
Graph gen_DAG(int point_count,int edge_count,bool self_loop=false,bool repeated_edges=false,bool loop=false,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
int edge_count -> the count of edges
bool self_loop = False -> whether to allow self loops or not
bool repeated_edges = True -> whether to allow repeated edges or not
bool loop = False -> whether to allow loops or not
pair<int,int> weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
*/
int ll=weight_limit.first,rr=weight_limit.second;
if(ll>rr)rr=ll;
set<pair<int,int>>used_edges;
auto edge_buf = gen_tree(point_count,1,0,0,weight_limit).iterate_edges();
auto graph = Graph(point_count,true);
for(auto &edge:edge_buf){
if(loop&&rnd(0,1)){
swap(edge.start,edge.end);
}
graph.add_edge(edge.start, edge.end, edge.weight);
if(!repeated_edges){
used_edges.insert({edge.start, edge.end});
}
}
int i = point_count - 1;
while(i < edge_count){
int u = rnd(1, point_count);
int v = rnd(1, point_count);
if(!loop&&u>v)swap(u,v);
if (!self_loop && u == v)continue;
if(!repeated_edges&&used_edges.find({u,v})!=used_edges.end())continue;
graph.add_edge(u,v,rnd(ll,rr));
if(!repeated_edges){
used_edges.insert({u,v});
}
i++;
}
return graph;
}
Graph gen_UDAG(int point_count,int edge_count,bool self_loop=false,bool repeated_edges=false,bool loop=false,pair<long long,long long>weight_limit={1,1}){
/*
int point_count -> the count of vertexes
int edge_count -> the count of edges
bool self_loop = False -> whether to allow self loops or not
bool repeated_edges = True -> whether to allow repeated edges or not
bool loop = False -> whether to allow loops or not
pair<int,int> weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
*/
int ll=weight_limit.first,rr=weight_limit.second;
if(ll>rr)rr=ll;
set<pair<int,int>>used_edges;
auto edge_buf = gen_tree(point_count,0,0,0,weight_limit).iterate_edges();
auto graph = Graph(point_count,true);
for(auto &edge:edge_buf){
if(loop&&rnd(0,1)){
swap(edge.start,edge.end);
}
graph.add_edge(edge.start, edge.end, edge.weight);
if(!repeated_edges){
used_edges.insert({edge.start, edge.end});
}
}
int i = point_count - 1;
while(i < edge_count){
int u = rnd(1, point_count);
int v = rnd(1, point_count);
if(!loop&&u>v)swap(u,v);
if (!self_loop && u == v)continue;
if(!repeated_edges&&used_edges.find({u,v})!=used_edges.end())continue;
graph.add_edge(u,v,rnd(ll,rr));
if(!repeated_edges){
used_edges.insert({u,v});
used_edges.insert({v,u});
}
i++;
}
return graph;
}
Graph gen_hack_spfa(int point_count,int edge_count,bool directed=false,pair<long long,long long>weight_limit={1,1},int extra_edge=2){
int ll=weight_limit.first,rr=weight_limit.second;
if(ll>rr)rr=ll;
int point_to_skip = point_count + 3;
auto graph = Graph(point_count, directed);
if (point_count % 2)
point_to_skip = point_count / 2 + 1;
int half = int(point_count / 2);
int x,y;
for(int i=1;i<half;i++){
x=i,y=i+1;
graph.add_edge(x + (x >= point_to_skip), y + (y >= point_to_skip), rnd(ll,rr));
x=i+half,y=x+1;
graph.add_edge(x + (x >= point_to_skip), y +(y >= point_to_skip), rnd(ll,rr));
}
for(int i=1;i<=half;i++){
x=i,y=i+half;
graph.add_edge(x + (x >= point_to_skip), y +(y >= point_to_skip), rnd(ll,rr));
}
for(int i=1;i<=extra_edge;i++){
int u = rnd(1,point_count);
int v =rnd(1,point_count);
graph.add_edge(u, v, rnd(ll,rr));
}
return graph;
}
}剪贴板
剪贴板 z6scwg9u
剪贴板信息
剪贴板信息
- 收录
- 更新