# 题目

Input

Σ
n 106;
Σ
m 2 106。
Output

Scoring

Page 2 of 5
CSP2019 Training jiangly Contest 1
High School Affiliated to Southwest University, Chongqing, 23 Oct 2019
Example
spfa.in spfa.out
3
1 0
1 1
1 1 a
4 6
1 2 i
1 3 a
1 4 k
2 3 i
2 4 o
3 4 i
0 a 1
1
-1
3 i 4
1 2 3 4
Note

# 题解

/*

*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
const int MAXN = 1e6 +3;
int n , m;
bool vis[MAXN];
int dp[MAXN][30];
int pre[MAXN][30];
int in[MAXN];
struct node{
int v;char s;
node(){}
node( int V , char W ){
v= V;s = W;
}
};
int siz[MAXN] , cnt;
vector<node>G[MAXN];
x = 0 ;char s = getchar();
while( s < '0' || s > '9' ){
s = getchar();
}
while( s >='0' && s <= '9' ){
x = x * 10 + s - '0';
s = getchar();
}

}
int tot , ans , last;
char c;
bool flag;
int ne[MAXN];
void TP( ){
queue<int>q;
for( int i = 1 ; i <= n ; i ++ ){
if( !in[i] )
q.push( i ) , ne[++cnt] = i;
}
while( !q.empty() ){
int x = q.front();q.pop();
for( int i = 0 ; i < G[x].size() ; i++ ){
int v = G[x][i].v;
if( --in[v] == 0 ){
q.push( v );
ne[++cnt] = v;
}
}
}
}
int sum[MAXN];
void print( int x , int y ){
if( x == pre[x][y] ){
tot ++;
sum[tot] = x;
return ;
}
print( pre[x][y], y );
tot ++;
sum[tot] = x;
}
int main()
{
//freopen( "spfa.in" , "r" , stdin );
//freopen( "spfa.out" , "w" , stdout );
int T;
scanf( "%d" , &T );
while( T -- ){
flag = 0;
scanf( "%d%d" , &n , &m );
for( int i = 1 ; i <= n ; i ++ ){
G[i].clear();
siz[i] = 0 , vis[i] =0 ;
sum[i] = 0;in[i] = 0;
for( int j = 1 ; j <= 26 ; j ++ )
pre[i][j] = i , dp[i][j] = 0;
}
tot = 0;
for( int i = 1 ; i <= m ; i ++ ){
int x , y;char p;
G[x].push_back( node( y , p ) );
in[y] +=1;
}
tot = 0;
ans = 0 , last = 1;
c = 'a';
cnt = 0;
TP();
if( cnt < n ){
printf( "-1\n" );
continue;
}
for( int i = 1 ; i <= n ; i ++ ){
int x = ne[i];
for( int j = 0 ; j < G[x].size() ; j ++ ){
int v = G[x][j].v , s = G[x][j].s - 96;
for( int k = 1 ; k <= 26 ; k ++ ){
if( dp[v][k] < dp[x][k] + ( s == k  ? 1 :0 ) ){
dp[v][k] =  dp[x][k] + ( s == k  ? 1 :0 );
pre[v][k] = x;
if( ans < dp[v][k] ){
ans = dp[v][k];c = k +96;
last = v;
}
}
}
}
}
printf( "%d %c" , ans , c );
if( !ans ){
printf( " 1\n1\n" );
continue;
}
else
print( last , c - 96 );
printf( " %d\n" , tot );
for( int i = 1 ; i <= tot ; i ++ )
printf( "%d " , sum[i] );
printf( "\n" );
}
return 0;
}


02-05 436

07-30 1631
09-01 1117
04-25 1085
03-27 1万+
05-12 4923
05-30 131
01-20 1万+
12-29 6682