QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#578290 | #9319. Bull Farm | fanhuaxingyu | WA | 1ms | 5696kb | C++20 | 6.5kb | 2024-09-20 18:01:23 | 2024-09-20 18:01:24 |
Judging History
answer
#include <bits/stdc++.h>
#include<array>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//#define int long long
#define lowbit(x) ((x) & (-x))
#define ar(x) array<int, x>
#define endl '\n'
#define all(x) begin(x),end(x)
#define all2(x) begin(x)+1,end(x)
using namespace std;
using i64 = long long;
const int N = 2e5 + 10;
const int M = 5e5 + 1;
const int INF = 0x3f3f3f3f;
const ll INF2 = 0x3f3f3f3f3f3f3f3f;
const long double PI = 3.1415926535897932384626;
const long double eps = 1e-5;
const ll mod = 19260817;
const ll mod2 = 998244353;
const int base = 131;
const int base2 = 13331;
int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
int dx2[] = { 2,-2,0,0 }, dy2[] = { 0,0,2,-2 };
ll qpow(ll a, ll b, ll p)
{
ll ans = 1;
while (b)
{
if (b & 1)
ans *= a, ans %= p;
a *= a;
a %= p;
b >>= 1;
}
return ans % p;
}
ll gcd(ll a, ll b)
{
return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b) * b;
}
int n, m;
//tim为时间
int dfn[N], low[N], tim = 0, vis[N], cnt = 0, top = 0;//dfn标记dfs遍历的顺序,low标记当前点通过某条边所能回溯到的最早的那个点,vis标记当前点是否在栈内
vector<int>g[2020];//邻接表存图和缩点后的图
int s[N];//栈
int scc[N];//缩点后原点属于缩点后的哪个点
void dfs(int x)
{
s[++top] = x;
vis[x] = true;//标记为在栈内
dfn[x] = low[x] = ++tim;//时间入和出的初标记
for (int i = 0; i < g[x].size(); ++i)
{
int y = g[x][i];
if (dfn[y] == 0)//如果当前点没被遍历
{
dfs(y);//遍历当前当前点
low[x] = min(low[x], low[y]);//更新low
}
else if (vis[y] == true)//如果当前点在栈内则更新low
{
low[x] = min(low[x], low[y]);
}
//如果不在栈内则不用管
}
if (dfn[x] == low[x])//如果当前二者相等
{
int t;
++cnt;
do {
t = s[top--];
vis[t] = 0;
scc[t] = cnt;
} while (t != x);
}
}
void tarjan()
{
for (int i = 1; i <= n; ++i)//循环dfs,因为可能不连通
{
if (dfn[i] == 0)
dfs(i);
}
}
void init()
{
top = tim = cnt = 0;
for (int i = 1; i <= n; ++i)
{
g[i].clear();
dfn[i] = scc[i] = low[i] = vis[i] = 0;
}
}
void solve()
{
int l, q;
cin >> n >> l >> q;
init();
vector<vector<int>>mp(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i)
mp[i][i] = 0;
for (int i = 1; i <= l; ++i)
{
string s;
cin >> s;
vector<int>cn(n + 1);
vector<int>t(n + 1);
for (int j = 1; j <= n; ++j)
{
t[j] = (s[2 * j - 2] - 48) * 50 + (s[2 * j - 1] - 48);
cn[t[j]]++;
}
int ok = 0;
for (int j = 1; j <= n; ++j)
{
if (cn[j] == 1)
{
ok++;
}
}
if (ok == n)
{
for (int j = 1; j <= n; ++j)
{
if (mp[j][t[j]] == INF)
g[j].push_back(t[j]);
mp[j][t[j]] = min(mp[j][t[j]], i);
if (mp[t[j]][j] == INF)
g[t[j]].push_back(j);
mp[t[j]][j] = min(mp[t[j]][j], i);
}
}
else if (ok == n - 2)
{
vector<int>tmp;
int tmp2 = 0;
for (int j = 1; j <= n; ++j)
if (cn[t[j]] == 2)
tmp.push_back(j);
for (int j = 1; j <= n; ++j)
if (cn[j] == 0)tmp2 = j;
if (mp[tmp[0]][tmp2] == INF)
g[tmp[0]].push_back(tmp2);
if (mp[tmp[1]][tmp2] == INF)
g[tmp[1]].push_back(tmp2);
mp[tmp[0]][tmp2] = min(mp[tmp[0]][tmp2], i);
mp[tmp[1]][tmp2] = min(mp[tmp[1]][tmp2], i);
}
}
tarjan();
vector<array<int, 3>>e;
for (int i = 1; i <= n; ++i)
{
for (int v : g[i])
{
e.push_back({ mp[i][v],i,v });
}
}
sort(all(e));
vector<int>fa(n + 1);
for (int i = 1; i <= n; ++i)
{
fa[i] = i;
}
function<int(int)>find = [&](int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
};
vector<vector<array<int,2>>>sg(n + 1);
for (auto [w, u, v] : e)
{
if (scc[u] == scc[v])
{
int a = find(u);
int b = find(v);
if (a != b)
{
fa[a] = b;
sg[u].push_back({ v, mp[u][v] });
sg[v].push_back({ u, mp[v][u] });
}
}
else
{
sg[u].push_back({ v,w });
}
}
vector<vector<int>>dis(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i)
dis[i][i] = 0;
auto dij = [&](int s)
{
vector<int>vis2(n + 1);
dis[s][s] = 0;
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>>pq;
pq.push({ 0,s });
while (pq.size())
{
auto [w, u] = pq.top();
pq.pop();
if (vis2[u])continue;
vis2[u] = 1;
for (auto [v, w2] : sg[u])
{
if (dis[s][v] > max(w, w2))
{
dis[s][v] = max(w, w2);
pq.push({ dis[s][v],v });
}
}
}
};
for (int i = 1; i <= n; ++i)
dij(i);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cout << i << " " << j << " " << dis[i][j] << endl;
}
}
vector<int>ans;
while (q--)
{
string s;
cin >> s;
int a, b, c;
a = (s[0] - 48) * 50 + (s[1] - 48);
b = (s[2] - 48) * 50 + (s[3] - 48);
c = (s[4] - 48) * 50 + (s[5] - 48);
if (dis[a][b] <= c)
ans.push_back(1);
else
ans.push_back(0);
}
for (int i = 0; i < ans.size(); ++i)
cout << ans[i];
cout << endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5696kb
input:
2 5 2 4 0305040201 0404040404 030300 020500 050102 020501 6 2 4 030603010601 010203060504 030202 060402 050602 060401
output:
1 1 0 1 2 1 1 3 1 1 4 1 1 5 1 2 1 1 2 2 0 2 3 1 2 4 1 2 5 1 3 1 1 3 2 1 3 3 0 3 4 1 3 5 1 4 1 1 4 2 1 4 3 1 4 4 0 4 5 1 5 1 1 5 2 1 5 3 1 5 4 1 5 5 0 1011 1 1 0 1 2 1061109567 1 3 1061109567 1 4 1061109567 1 5 1061109567 1 6 1061109567 2 1 1061109567 2 2 0 2 3 1061109567 2 4 1061109567 2 5 106110956...
result:
wrong answer 1st lines differ - expected: '1011', found: '1 1 0'