AquaDaMean1e @ 2024-09-13 20:22:42
#include <bits/stdc++.h>
using namespace std;
struct edge {
int next, ed;
}b[1000010];
struct node {
int x, step;
};
queue <node> q;
int n, Q, t[1010], nbs[1010], tot;
bool vis1[1010], vis2[1010][1010];
int read () {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch - '0');
ch = getchar();
}
return x * f;
}
void print (int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
int bfs () {
int ret = 0;
for (int i = 1; i <= n; i++) {
if (!t[i]) {
q.push({i, 1});
}
}
while (!q.empty()) {
node cur = q.front();
ret = max(ret, cur.step);
for (int k = nbs[cur.x]; k; k = b[k].next) {
int u = b[k].ed;
t[u]--;
if (!t[u]) {
q.push({u, cur.step + 1});
}
}
q.pop();
}
return ret;
}
int main () {
n = read(), Q = read();
while (Q--) {
memset(vis1, 0, sizeof(vis1));
int s, l, r;
s = read();
for (int i = 1, x; i <= s; i++) {
x = read();
if (i == 1) {
l = x;
}
if (i == s) {
r = x;
}
vis1[x] = true;
}
for (int i = l; i <= r; i++) {
if (vis1[i]) {
for (int j = l; j <= r; j++) {
if (!vis1[j] && !vis2[i][j]) {
vis2[i][j] = true;
t[j]++;
b[++tot].ed = j; b[tot].next = nbs[i]; nbs[i] = tot;
}
}
}
}
}
print(bfs());
return 0;
}
心态崩了
by 鱼跃于渊 @ 2024-09-15 17:55:04
@AquaDaMean1e 三次方的做法是肯定过不了的。
by __D_A_T__ @ 2024-09-15 17:56:27
@AquaDaMean1e 感觉你的建图部分怪怪的,看看我的:
#include<bits/stdc++.h>
using namespace std;
int n,m,k,ans;
int a[1005],dp[1005],in[1005];
bool vis[1005],ad[1005][1005];
struct edge
{
int to,next;
}e[1000005];
int h[1005],cnt;
void add(int x,int y)
{
e[++cnt].to=y;
e[cnt].next=h[x];
h[x]=cnt;
}
void topo()
{
queue<int>q;
for(int i=1;i<=n;i++)
if(!in[i])q.push(i),dp[i]=1;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=h[x];i;i=e[i].next)
{
dp[e[i].to]=dp[x]+1,ans=max(ans,dp[e[i].to]);
if(!(--in[e[i].to]))q.push(e[i].to);
}
}
}
signed main()
{
scanf("%d%d",&n,&m);
while(m--)
{
memset(vis,0,sizeof(vis));
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d",a+i);
vis[a[i]]=1;
}
for(int i=a[1];i<=a[k];i++)
if(!vis[i])
for(int j=1;j<=k;j++)
if(!ad[i][a[j]])
ad[i][a[j]]=1,in[a[j]]++,add(i,a[j]);
}
topo();
printf("%d\n",ans);
return 0;
}
by AquaDaMean1e @ 2024-09-15 18:04:57
@D_A_T @鱼跃于渊 thx.已AC
by CppHZ @ 2024-10-01 22:55:52
你这 O(n^3) 肯定过不了啊建议重新考虑时间复杂度