首页 > 代码库 > Gym 101142C CodeCoder vs TopForces (搜索)

Gym 101142C CodeCoder vs TopForces (搜索)

题意:每个人有2种排名,对于A只要有一种排名高于B,那么A就能赢B,再如果B能赢C,那么A也能赢C,要求输出每个人分别能赢多少个人

析:首先把题意先读对了,然后我们可以建立一个图,先按第一种排名排序,然后从高的向向低的连一条边,然后再按第二种排序,同理连线。

最后dfs一次,要先从排名低的开始遍历,不用清0,因为是从排名低的开始的。也可以用强连通分量或者线段树。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> G[maxn];
struct Node{
  int x, y, id;
};
Node a[maxn];

bool cmp1(const Node &lhs, const Node &rhs){
  return lhs.x < rhs.x;
}

bool cmp2(const Node &lhs, const Node &rhs){
  return lhs.y < rhs.y;
}
int cnt;
int ans[maxn];
bool vis[maxn];

void dfs(int u){
  if(vis[u])  return ;
  ++cnt;
  vis[u] = true;
  for(int i = 0; i < G[u].size(); ++i)  dfs(G[u][i]);
}

int main(){
  freopen("codecoder.in", "r", stdin);  
  freopen("codecoder.out", "w", stdout); 
  scanf("%d", &n);
  for(int i = 0; i < n; ++i){
    scanf("%d %d", &a[i].x, &a[i].y);
    a[i].id = i;
  }
  sort(a, a + n, cmp1);
  for(int i = n-1; i > 0; --i)  G[a[i].id].push_back(a[i-1].id);
  sort(a, a + n, cmp2);
  for(int i = n-1; i > 0; --i)  G[a[i].id].push_back(a[i-1].id);
  cnt = 0;
  for(int i = 0; i < n; ++i){
    dfs(a[i].id);
    ans[a[i].id] = cnt - 1;
  }
  for(int i = 0; i < n; ++i)  printf("%d\n", ans[i]);
  return 0;
}

  

Gym 101142C CodeCoder vs TopForces (搜索)