最近寫了一個 GRAPH_GEN.h 檔,想說可以方便生成測資。以下為程式碼
前三個會回傳 vector<pair<int,int>>
每個皆有保證沒有自環,後三個則會回傳 vector<edge>
,以節省重複打 first
與 second
的時間
#include<bits/stdc++.h>
using namespace std;
#ifndef GRAPH_GEN
#define GRAPH_GEN
unsigned seed=chrono::steady_clock().now().time_since_epoch().count();
mt19937_64 rng(seed);
using ll=long long;
ll Rand(ll a,ll b){
uniform_int_distribution<ll> dis(a,b);
return dis(rng);
}
ll Rand(ll a){
return Rand(1,a);
}
struct edge{
int from,to;
long long dis;
edge(){}
edge(int f,int t,long long d){
from=f;
to=t;
dis=d;
}
};
class GraphGen{
private :
struct DSU{
vector<int> dsu,rk;
DSU(int n){
dsu.resize(n+10);
rk.resize(n+10);
init();
}
void init(){
for(int i=0;i<dsu.size();++i){
dsu[i]=i;
}
}
int find(int a){
if(dsu[a]==a){
return a;
}else{
return dsu[a]=find(dsu[a]);
}
}
bool same(int a,int b){
return find(a)==find(b);
}
void uni(int a,int b){
if(same(a,b)){
return;
}
if(rk[find(a)]==rk[find(b)]){
dsu[find(b)]=find(a);
rk[a]++;
}else if(rk[find(a)]>rk[find(b)]){
dsu[find(b)]=find(a);
}else{
dsu[find(a)]=find(b);
}
}
};
public :
// nodes 1~n
static vector<pair<int,int>> GenTree(int n){
DSU dsu(n);
vector<pair<int,int>> result;
while(result.size()<n-1){
int a=Rand(n),b=Rand(n);
if(!dsu.same(a,b)){
result.emplace_back(a,b);
dsu.uni(a,b);
}
}
return result;
}
// n nodes m edges, m need to bigger than n-1
static vector<pair<int,int>> GenConnectedGraph(int n,int m){
vector<pair<int,int>> result=GenTree(n);
for(int i=0;i<m-n+1;++i){
int a,b;
do{
a=Rand(n);
b=Rand(n);
}while(a==b);
result.emplace_back(a,b);
}
return result;
}
// n nodes m edges, m need to bigger than n-1
static vector<pair<int,int>> GenGraph(int n,int m){
vector<pair<int,int>> result;
for(int i=0;i<m;++i){
int a,b;
do{
a=Rand(n);
b=Rand(n);
}while(a==b);
result.emplace_back(a,b);
}
return result;
}
// nodes 1~n, and weight in 1~k
static vector<edge> GenTree(int n,int k){
DSU dsu(n);
vector<edge> result;
while(result.size()<n-1){
int a=Rand(n),b=Rand(n),c=Rand(k);
if(!dsu.same(a,b)){
result.emplace_back(a,b,c);
dsu.uni(a,b);
}
}
return result;
}
static vector<edge> GenConnectedGraph(int n,int m,int k){
vector<edge> result=GenTree(n,k);
for(int i=0;i<m-n+1;++i){
int a,b;
do{
a=Rand(n);
b=Rand(n);
}while(a==b);
int c=Rand(k);
result.emplace_back(a,b,c);
}
return result;
}
static vector<edge> GenGraph(int n,int m,int k){
vector<edge> result;
for(int i=0;i<m;++i){
int a,b;
do{
a=Rand(n);
b=Rand(n);
}while(a==b);
int c=Rand(k);
result.emplace_back(a,b,c);
}
return result;
}
};
#endif
#include<bits/stdc++.h>
#include "GRAPH_GEN.h"
using namespace std;
int main(){
vector<pair<int,int>> edges=GraphGen::GenTree(100000);
}
定義子問題。
Dec 20, 2024給定一個帶權圖G,求一條路徑讓S到T的權重總和最小。
Aug 9, 2023#include<bits/stdc++.h> using namespace std; unsigned seed=chrono::steady_clock().now().time_since_epoch().count(); mt19937_64 rng(seed); struct Treap{ struct node{ int key,val,pos,pri,sz; int mx,mxPos;
Jun 17, 2023題目敘述 神獸日京元帶著得意門生黃瓜學長去科博館,在館外有一個裝置,內有多個球不斷的被送進一個"單一"開口的管子,而過了一段時間後,系統會將管子傾斜並將部分的球送出。而現在正在舉辦一個活動,主辦單位將球編號(1,2,3...,n),而參加者要控制系統並將球經過操作後排成特定的順序,完成者能免費進入科博館。黃瓜學長作為一個資訊高手aka厭惡零錢大鈔主義者,又不想被坑錢,他必須完成目標。 輸入說明 第一行為一整數$n(0<n<10^5)$ 第二行有$n$個正數$a_1,a_2,....a_n(1 \le a_i \le n)$ 輸出說明 求第二行之排序有沒有可能達成,有的話輸出"Yes",否則輸出"No"
Jun 17, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up