zoukankan      html  css  js  c++  java
  • 信息竞赛进阶指南--搜索相关(模板)

    ACM常用模板合集

    // 深度优先遍历框架
    void dfs(int x) {
    	v[x] = 1;
    	for (int i = head[x]; i; i = next[i]) {
    		int y = ver[i];
    		if (v[y]) continue;
    		dfs(y);
    	}
    }
    
    // DFS序
    void dfs(int x) {
    	a[++m] = x;
    	v[x] = 1;
    	for (int i = head[x]; i; i = next[i]) {
    		int y = ver[i];
    		if (v[y]) continue;
    		dfs(y);
    	}
    	a[++m] = x;
    }
    
    // 求树中各点的深度
    void dfs(int x) {
    	v[x] = 1;
    	for (int i = head[x]; i; i = next[i]) {
    		int y = ver[i];
    		if (v[y]) continue; // 点y已经被访问过了
    		d[y] = d[x] + 1;
    		dfs(y);
    	}
    }
    
    // 求树的重心
    void dfs(int x) {
    	v[x] = 1; size[x] = 1; // 子树x的大小
    	int max_part = 0; // 删掉x后分成的最大子树的大小
    	for (int i = head[x]; i; i = next[i]) {
    		int y = ver[i];
    		if (v[y]) continue; // 点y已经被访问过了
    		dfs(y);
    		size[x] += size[y];
    		max_part = max(max_part, size[y]);
    	}
    	max_part = max(max_part, n - size[x]);
    	if (max_part < ans) {
    		ans = max_part;
    		pos = x;
    	}
    }
    
    // 划分图的连通块
    void dfs(int x) {
    	v[x] = cnt;
    	for (int i = head[x]; i; i = next[i]) {
    		int y = ver[i];
    		if (v[y]) continue;
    		dfs(y);
    	}
    }
    for (int i = 1; i <= n; i++)
    	if (!v[i]) {
    		cnt++;
    		dfs(i);
    	}
    
    // 广度优先遍历框架
    void bfs() {
    	memset(d, 0, sizeof(d));
    	queue<int> q;
    	q.push(1); d[1] = 1;
    	while (q.size()) {
    		int x = q.front(); q.pop();
    		for (int i = head[x]; i; i = next[i]) {
    			int y = ver[i];
    			if (d[y]) continue;
    			d[y] = d[x] + 1;
    			q.push(y);
    		}
    	}
    }
    
    // 拓扑排序
    void add(int x, int y) { // 在邻接表中添加一条有向边
    	ver[++tot] = y, next[tot] = head[x], head[x] = tot;
    	deg[y]++;
    }
    void topsort() {
    	queue<int> q;
    	for (int i = 1; i <= n; i++)
    		if (deg[i] == 0) q.push(i);
    	while (q.size()) {
    		int x = q.front(); q.pop();
    		a[++cnt] = x;
    		for (int i = head[x]; i; i = next[i]) {
    			int y = ver[i];
    			if (--deg[y] == 0) q.push(y);
    		}
    	}
    }
    int main() {
    	cin >> n >> m; // 点数、边数
    	for (int i = 1; i <= m; i++) {
    		int x, y;
    		scanf("%d%d", &x, &y);
    		add(x, y);
    	}
    	topsort();
    	for (int i = 1; i <= cnt; i++)
    		printf("%d ", a[i]);
    	cout << endl;
    }
    
  • 相关阅读:
    tps吞吐量映射的问题
    深浅拷贝
    webdriver驱动火狐浏览器报错:Unable to find a matching set of capabilities
    xpath元素定位方法
    linux监控系统性能命令
    测试质量评估
    测试策略模型设计
    需求评审
    安全测试的目的,发现哪些问题
    url组成
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798244.html
Copyright © 2011-2022 走看看