`

Uva 11419 - SAM I AM(最大匹配模板、找出最小覆盖点模板)

 
阅读更多

连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=560&problem=2414&mosmsg=Submission+received+with+ID+11685942

 

Problem C
SAM I AM
Input: Standard Input

Output: Standard Output

 

The world is in great danger!! Mental's forces have returned to Earth to eradicate humankind. Our last hope to stop this great evil is Sam “Serious” Stone. Equipped with various powerful weapons, Serious Sam starts his mission to destroy the forces of evil.

After fighting two days and three nights, Sam is now in front of the temple KOPTOS where Mental's general Ugh Zan III is waiting for him. But this time, he has a serious problem. He is in shortage of ammo and a lot of enemies crawling inside the temple waiting for him. After rounding thetemple Sam finds that the temple is in rectangle shape and he has the locations of all enemies in the temple.

C:\Documents and Settings\Angel of Death\Desktop\sam grid.gifAll of a sudden he realizes that he can kill the enemies without entering the temple using the great cannon ball which spits out a gigantic ball bigger than him killing anything it runs into and keeps on rolling until it finally explodes. But the cannonball can only shoot horizontally or vertically and all the enemies along the path of that cannon ball will be killed.

Now he wants to save as many cannon balls as possible for fighting with Mental. So, he wants to know the minimum number of cannon balls and the positions from which he can shoot the cannonballs to eliminate all enemies from outside that temple.

 

Input

Here, the temple is defined as a RXC grid. The first line of each test case contains 3 integers: R(0<R<1001), C(0<C<1001) representing the grid of temple (R means number of row and C means number of column of the grid) and the number of enemies N(0<N<1000001) inside the temple. After that there are N lines each of which contains 2 integers representing the position of the enemies in that temple. Each test case is followed by a new line (except the last one). Input is terminated when R=C=N=0. The size of the input file is around 1.3 MB.

 

Output

For each test case there will be one line output. First print the minimum number (m) of cannonballs needed to wipe out the enemies followed by a single space and then m positions from which he can shoot those cannonballs. For shooting horizontally print “r” followed by the row number and for vertical shooting print “c” followed by the column number. If there is more than one solution any one will do.

 

Sample Input                               Output for Sample Input

 

4 3

1

1 4

3 2

 

4 2

1

2

 

0 0

 

2 r1 r3

2 r1 r2

 

 

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1000 + 5; // 单侧顶点的最大数目

// 二分图最大基数匹配
struct BPM {
  int n, m;               // 左右顶点个数
  vector<int> G[maxn];    // 邻接表
  int left[maxn];         // left[i]为右边第i个点的匹配点编号,-1表示不存在
  bool T[maxn];           // T[i]为右边第i个点是否已标记

  int right[maxn];        // 求最小覆盖用
  bool S[maxn];           // 求最小覆盖用

  void init(int n, int m) {
    this->n = n;
    this->m = m;
    for(int i = 0; i < n; i++) G[i].clear();
  }

  void AddEdge(int u, int v) {
    G[u].push_back(v);
  }

  bool match(int u){
    S[u] = true;
    for(int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (!T[v]){
        T[v] = true;
        if (left[v] == -1 || match(left[v])){
          left[v] = u;
          right[u] = v;
          return true;
        }
      }
    }
    return false;
  }

  // 求最大匹配
  int solve() {
    memset(left, -1, sizeof(left));
    memset(right, -1, sizeof(right));
    int ans = 0;
    for(int u = 0; u < n; u++) { // 从左边结点u开始增广
      memset(S, 0, sizeof(S));
      memset(T, 0, sizeof(T));
      if(match(u)) ans++;
    }
    return ans;
  }

  // 求最小覆盖。X和Y为最小覆盖中的点集
  int mincover(vector<int>& X, vector<int>& Y) {
    int ans = solve();
    memset(S, 0, sizeof(S));
    memset(T, 0, sizeof(T));
    for(int u = 0; u < n; u++)
      if(right[u] == -1) match(u); // 从所有X未盖点出发增广
    for(int u = 0; u < n; u++)
      if(!S[u]) X.push_back(u); // X中的未标记点
    for(int v = 0; v < m; v++)
      if(T[v]) Y.push_back(v);  // Y中的已标记点
   return ans;
  }
};

BPM solver;

int R, C, N;

int main(){
  int kase = 0;
  while(scanf("%d%d%d", &R, &C, &N) == 3 && R && C && N) {
    solver.init(R, C);
    for(int i = 0; i < N; i++) {
      int r, c;
      scanf("%d%d", &r, &c); r--; c--;
      solver.AddEdge(r, c);
    }
    vector<int> X, Y;
    int ans = solver.mincover(X, Y);//把结果放在X,Y里面
    printf("%d", ans);
    for(int i = 0; i < X.size(); i++) printf(" r%d", X[i]+1);
    for(int i = 0; i < Y.size(); i++) printf(" c%d", Y[i]+1);
    printf("\n");
  }
  return 0;
}

 

 

 

 

 

 

 

 

 

 

 

1
1
分享到:
评论

相关推荐

    Uva 1510 - Neon Sign

    例如,对于第`i`个角点,第`i`行将给出从角点`i`到角点`i+1`一直到角点`N`的发光管颜色。 ##### 输出数据 对于每个测试用例,输出一行包含一个整数,即霓虹灯招牌上可以形成的同色三角形的数量。 #### 解题思路 ...

    UVA100~200---52道题accept代码,均顺利accept过

    6. **UVA128 - Buses**:这是一个时间表匹配问题,需要找出两路公交车在哪个站点相遇。可以使用线性搜索或二分查找算法。 7. **UVA195 - Dwarves and Gems**:这题涉及到贪心算法,要求分配宝石给矮人,使总价值...

    UVaOJ-401(Palindromes).zip_401 Palindromes

    标题中的"UVaOJ-401(Palindromes)"表明这是一个关于解决UVa Online Judge(UVa OJ)上编号为401的编程挑战,该挑战的主题是"Palindromes",即回文串。回文串是指一个字符串无论从前读到后还是从后读到前都是相同的,...

    uva705-Slash-Maze-.rar_Slash_uva705

    【标题】"uva705-Slash-Maze-.rar_Slash_uva705" 指向的是一个在UVa Online Judge (UVa OJ) 上提交并通过的编程问题,具体为问题编号705,名为"Slash Maze"。这个压缩包很可能包含了该问题的解决方案源代码。 ...

    开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip

    开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…

    Algorithm-UVA-Solutions-in-Python.zip

    "Algorithm-UVA-Solutions-in-Python.zip"这个压缩包文件正是针对UVA竞赛中问题的Python 3解决方案集合。 Python作为一门易学且功能强大的编程语言,因其简洁的语法和丰富的库支持,成为了许多算法爱好者和开发者的...

    UVA133-TheDoleQueue.zip_site:www.pudn.com_uva133

    堆是一种特殊的树形数据结构,满足堆属性:父节点的值总是大于或等于(最小堆)或小于或等于(最大堆)其子节点的值。在这个问题中,我们可以用最小堆来存储失业者,其中堆顶元素代表等待时间最短的失业者。每次处理...

    uva532-Dungeon-Master.rar_dungeon

    理解并分析代码的时间复杂度和空间复杂度,找出性能瓶颈,进行必要的优化,如使用数据结构的优化、减少冗余计算等,都是程序员必备的技能。 通过深入研究"uva532 Dungeon Master"的源代码,我们不仅能学习到游戏...

    tpcw-nyu-uva-client 客户端

    "tpcw-nyu-uva-client 客户端"是一个专为TPCW(Transaction Processing Performance Council Workloads)设计的应用程序,由纽约大学(NYU)和弗吉尼亚大学(UVA)共同开发。这个客户端软件主要用于模拟和评估数据库...

Global site tag (gtag.js) - Google Analytics