题目
题解思路
讲每个列当成连通的边,将行作为连通的点。
这样对每个行都有对应的坐标,进行降维处理。
出现列的时候肯定是让上下的行连通,出现行的时候,即让左右的行连通。
并查的时候查询是否已经是个连通块即可。
AC代码
#include <bits/stdc++.h>
//#include <unordered_map>
//priority_queue
#define PII pair<int,int>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 200100;
int n , m ;
int a[2010*2010] ;
int find(int x )
{
if ( x != a[x] )
a[x] = find(a[x]) ;
return a[x] ;
}
bool uio(int x , int y )
{
int fx = find(x) ;
int fy = find(y) ;
if ( fx != fy )
{
a[fy] = fx ;
return 0 ;
}
return 1 ;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> m ;
for (int i = 0 ; i <= (n+1)*(n+1) ; i++ )
a[i] = i ;
int falg = 0 ;
for (int i = 1 ; i <= m ; i++ )
{
int t1 , t2 ;
char ch[2] ;
cin >> t1 >> t2 >> ch ;
if ( falg )
continue;
if ( ch[0] == 'D' )
{
if ( uio(t1*n+t2,(t1+1)*n+t2) )
{
cout << i << "\n";
falg = 1 ;
}
}else
{
if ( uio(t1*n+t2,t1*n+t2+1))
{
cout << i << "\n" ;
falg = 1 ;
}
}
}
if ( !falg )
cout << "draw\n" ;
return 0 ;
}