LA 3523 圆桌骑士

来源:互联网 发布:wifi破解mac版 编辑:程序博客网 时间:2024/04/30 01:06
每一部分知识简单,合起来就容易出错了,慢慢敲,快了错误率以指数级别
#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<stack>#include<iostream>#include<vector>#include<algorithm>using namespace std;typedef long long ll;const int MAXN = 1007;bool g[MAXN][MAXN];bool odd[MAXN];bool iscut[MAXN];struct edge {int u, v;};int bln[MAXN];int low[MAXN], dfn[MAXN];int color[MAXN];int nb, pt;vector<int> G[MAXN], bl[MAXN];stack<edge> sg;int dfs(int u, int fa) {dfn[u] = low[u] = ++pt;int i, child = 0, n = G[u].size();for(i = 0; i < n; i++) {int v = G[u][i];edge e = (edge) {u, v};if( !dfn[v] ) {child ++;sg.push(e);low[u] = min(low[u], dfs(v, u));if(low[v] >= dfn[u]) {iscut[u] = true;nb ++;bl[nb].clear();while( !sg.empty()) {edge tmp = sg.top();sg.pop();if( bln[tmp.u] != nb ) {bl[nb].push_back(tmp.u);bln[tmp.u] = nb;}if( bln[tmp.v] != nb) {bl[nb].push_back(tmp.v);bln[tmp.v] = nb;}if(tmp.u == u && tmp.v == v) break;}}}else if(dfn[v] < dfn[u] && v != fa) {sg.push(e);low[u] = min(low[u], dfn[v]);}}if( fa == 0 && child < 2) iscut[u] = false;return low[u];}void getbl(int n) {nb = pt = 0;while( !sg.empty() ) sg.pop();memset(dfn, 0, sizeof(dfn));memset(bln, 0, sizeof(bln));memset(iscut, 0, sizeof(iscut));int i;for(i = 1; i <= n; i++)if( !dfn[i] ) dfs(i, 0);}bool hwmatch(int u, int cnt) {int i, vn = G[u].size();for(i = 0; i < vn; i++) {int v = G[u][i];if(bln[v] != cnt) continue;if( color[u] == color[v] ) return false;if( !color[v] ) {color[v] = 3 - color[u];if( !hwmatch(v, cnt) ) return false;}}return true;}int main() {int n, m;while(~scanf("%d%d", &n ,&m), n|m) {memset(g, 0, sizeof(g));int u, v;while(m --) {scanf("%d%d", &u, &v);g[u][v] = g[v][u] = true;}int i, j;for(i = 1; i <= n; i++) G[i].clear();for(i = 1; i <= n; i++) {for(j = i + 1; j <= n; j++) {if( !g[i][j] ) {G[i].push_back(j);G[j].push_back(i);}}}getbl(n);memset(odd, 0, sizeof(odd));for(i = 1; i <= nb; i++) {memset(color, 0, sizeof(color));int vn = bl[i].size();for(j = 0; j < vn; j++) {bln[ bl[i][j] ] = i;}color[ bl[i][0] ] = 1;if( !hwmatch(bl[i][0], i) ) {for(j = 0; j < vn; j++) {odd[ bl[i][j] ] = true;}}}int ans = n;for(i = 1; i <= n; i ++) {if(odd[i]) ans --;}printf("%d\n", ans);}return 0;}

上升
原创粉丝点击