#include <iostream>#include <stdio.h>#include <string.h> #define MAX 20using namespace std;struct edge{ int u, v, w;}edges[MAX];int parent[MAX];int n, m;int i, j;void UFset( ){ for( i = 1; i <= n; i++ ) parent[i] = -1;
題意:有幾個房間,從一個房間開始走,進入另一個房間的要求是燈要亮著,而且兩個房間之間是連通的。題目會給出房間的控制開關,也就是哪個房間的開關可以控制哪一個房間的開關。輸入有房間數,房間的串連數,和開關控制數,要求從走廊開始,走到臥室,而且最後只有臥室的燈是開著的,別的房間的燈都是關著的那麼這道題要注意的是,一個操作是一步,要求輸出走到臥室的最少操作,以及操作的順序關鍵在於建圖和判重,其他的就是BFS的模板即可由於要記錄操作順序,所以節點的結構設定也比較重要 The New Villa Mr.
/*好長時間沒有A題了,寫個簡單搜尋連memset( map, 0, sizeof(map) )都忘了,TLE了2次...唉...*/#include <stdio.h>#include <string.h>#define MAX 100int map[MAX][MAX], max;int vis[MAX][MAX];int node, elem;void dfs( int n, int cal ){int i;for( i = 0; i < node; i++ )
這個題目的意思就是用兩個顏色給圖中每個點塗色,使相鄰的點顏色不同。如果能有一個這樣的方案,那麼就輸出"BICOLORING." 否則輸出"NOT BICOLORING."。 資料規模是n最大為200.很典型的搜尋題目代碼如下:#include <cstdio>#include <cstring>const int N = 210;int n, l, map[N][N];int vis[N];bool dfs( int x ) { for ( int i = 0;
這道題很顯然是拓撲排序,很水的題。一種是dfs方法,另一種是不用dfs代碼如下:dfs:#include <cstdio>#include <cstring>const int N = 110;int n, m, c[N], topo[N], t, map[N][N];bool dfs(int u) { c[u] = -1; for ( int v = 1; v <= n; ++v ) if( map[u][v] ) { if ( c[v]
很顯然,資料這麼小,dfs必然會過的 按上下左右搜,然後搜過的標記一下,避免重複,就哦啦代碼如下:#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N = 55;int n, m, id;char g[N][N];bool vis[N][N];struct node { char x; int
這道題,看題目就是知道了,就是求全圖的最小割。代碼是我在其他人的部落格上看到的,感覺很好,可以用做模板,就算是一個新的模板代碼如下://Stoer-Wagner#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int N = 550;const int INF = 0x3fffffff;int n,