版权声明: https://blog.csdn.net/moon_sky1999/article/details/81989029
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5961
个人认为,这道题的时间限制应该改为1000ms,6000ms导致大量O( n^3 )的暴力代码就这样水过去了。
思路:
首先考虑,如果一个竞赛图存在环,那么它一定不满足传递性。对于P是这样,对于Q也是这样。
如果存在一个环,其一部分有P中的边组成,另一部分由Q组成。若P满足传递性,则存在一条路径,与Q部分的边集的方向相反。由竞赛图的性质可得,两点间不会同时存在P中的边和Q中的边。
因此,第一步即是判断P并Q中是否有环,因为环可能是P中的或是Q中的或是PQ结合形成的。
第二步判断P并 Q的反向 中是否有环,原因和上述类似,两点间不可能会有同时在P中Q中的边。
代码(复杂度O(n^2)):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define ull unsigned long long
#define BUG printf("************\n")
using namespace std;
const ll mod = 1e9 + 7;
const int maxn = 2020;
const int maxm = 1e6 + 10;
const double eps = 1e-8;
ll gcd(ll a,ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a,ll b) {
return a / gcd(a, b) * b;
}
ll pow_mod(ll a, ll b, ll mod) {
ll ans = 1;
while (b) {
if (b & 1)ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans % mod;
}
bool is_prime(ll n) {
if (n == 1 || n == 2)return 1;
for (int i = 2; i <= sqrt(n); ++i)
if (n % i == 0)return 0;
return 1;
}
int n, du[maxn];
char c[maxn][maxn];
bool g[maxn][maxn];
queue<int> q;
bool check() {
memset(du, 0, sizeof(du));
int tot = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (g[i][j])
du[j]++;
}
}
for (int i = 1; i <= n; ++i) {
if (du[i] == 0)q.push(i);
}
while (!q.empty()) {
int x = q.front();
q.pop();
tot++;
for (int i = 1; i <= n; ++i) {
if (g[x][i]) {
du[i]--;
if (du[i] == 0)q.push(i);
}
}
}
return tot == n;
}
int main() {
/*ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
int _;
scanf("%d", &_);
while (_--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", c[i] + 1);
}
memset(g, 0, sizeof(g));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (c[i][j] == 'P' || c[i][j] == 'Q') {
g[i][j] = 1;
}
}
}
if (!check()) {
puts("N");
continue;
}
memset(g, 0, sizeof(g));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (c[i][j] == 'P' || c[j][i] == 'Q')
g[i][j] = 1;
}
}
if (!check()) {
puts("N");
continue;
} else {
puts("T");
}
}
return 0;
}
比赛的时候可以用随机数算法乱搞,时间限制比较宽松的时候可以取今可能多的随机数。
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define ull unsigned long long
#define BUG printf("************\n")
using namespace std;
const ll mod = 1e9 + 7;
const int maxn = 2020;
const int maxm = 1e6 + 10;
const double eps = 1e-8;
ll gcd(ll a,ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a,ll b) {
return a / gcd(a, b) * b;
}
ll pow_mod(ll a, ll b, ll mod) {
ll ans = 1;
while (b) {
if (b & 1)ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans % mod;
}
bool is_prime(ll n) {
if (n == 1 || n == 2)return 1;
for (int i = 2; i <= sqrt(n); ++i)
if (n % i == 0)return 0;
return 1;
}
int n, du[maxn];
char c[maxn][maxn];
bool g[maxn][maxn];
bool check() {
int x, y, z;
for (int i = 1; i <= 5000000; ++i) {
x = rand() % (n + 1);
y = rand() % (n + 1);
z = rand() % (n + 1);
if (c[x][y] == 'P' && c[y][z] == 'P' && c[x][z] != 'P')return 0;
if (c[x][y] == 'Q' && c[y][z] == 'Q' && c[x][z] != 'Q')return 0;
}
return 1;
}
int main() {
/*ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
srand(time(0));
int _;
scanf("%d", &_);
while (_--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", c[i] + 1);
}
if (check())puts("T");
else puts("N");
}
return 0;
}