Asteroids
Time Limit: 1000MS | Memory Limit: 65536K | |
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Line 1: Two integers N and K, separated by a single space.
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 4 1 1 1 3 2 2 3 2
Sample Output
2
————————————————————寝る前の分割線————————————————————
思路:水题不解释,注意行列编号从1开始。
dfs版代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 555, M = 11111;
int tot, head[N], cou[N], n, m;
bool vis[N];
struct Edge {
int v, next;
Edge(){}
Edge(int _v, int _next):
v(_v), next(_next){}
}edge[M];
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v)
{
edge[tot] = Edge(v, head[u]);
head[u] = tot++;
}
int dfs(int u)
{
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(!vis[v]) {
vis[v] = 1;
if(cou[v] == -1 || dfs(cou[v])) {
cou[v] = u;
return 1;
}
}
}
return 0;
}
int match()
{
int ret = 0;
memset(cou, -1, sizeof(cou));
for(int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
ret += dfs(i);
}
return ret;
}
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
while(~scanf("%d%d", &n, &m)) {
int u, v;
init();
for(int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
add(u, v);
}
int ans = match();
printf("%d\n", ans);
}
return 0;
}
bfs版代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 555, M = 11111;
int tot, head[N], mx[N], my[N], pre[N], n, m;
int vis[N];
struct Edge {
int v, next;
Edge(){}
Edge(int _v, int _next):
v(_v), next(_next){}
}e[M];
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v)
{
e[tot] = Edge(v, head[u]);
head[u] = tot++;
}
bool bfs(int st)
{
queue <int> q;
q.push(st);
pre[st] = -1;
bool flag = false;
while(!q.empty() && !flag) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i && !flag; i = e[i].next) {
int v = e[i].v;
if(vis[v] != st) {
vis[v] = st;
q.push(my[v]);
if(~my[v]) pre[my[v]] = u;
else {//找到增广路
int a = u, b = v;
flag = true;
while(~a) {
int t = mx[a];
mx[a] = b;
my[b] = a;
a = pre[a];
b = t;
}
}
}
}
}
return mx[st] != -1;
}
int hungary()
{
int ret = 0;
memset(mx, -1, sizeof(mx));
memset(my, -1, sizeof(my));
memset(vis, -1, sizeof(vis));
for(int i = 1; i <= n; i++) {//number from 1
if(mx[i] == -1) {
if(bfs(i)) ret++;
}
}
return ret;
}
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
while(~scanf("%d%d", &n, &m)) {
int u, v;
init();
for(int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
add(u, v);
}
int ans = hungary();
printf("%d\n", ans);
}
return 0;
}