fork download
  1. // ROOT : DRAGON3012009 : WA in Real Life
  2. #include <bits/stdc++.h>
  3. #define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
  4. #define FORD(i,r,l) for(int i = r ; i >= l ; i --)
  5. #define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
  6. #define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
  7. #define ll int
  8. #define el "\n"
  9. #define fi first
  10. #define se second
  11. #define _ROOT_ int main()
  12. #define M 1000000007
  13. #define MAXN 1000001
  14. #define INF (1ll<<30)
  15. #define NAME "file"
  16. #define debug(a) cout << #a << " = " << a << endl;
  17. using namespace std;
  18.  
  19. ll n, m, q ;
  20. ll a[MAXN] ;
  21. ll lab[MAXN ] ;
  22. vector<ll> adj[MAXN ] ;
  23. vector<pair<ll,ll>> history ;
  24.  
  25. ll find_set(ll a ) {
  26. return lab[a] < 0 ? a : find_set(lab[a]) ;
  27. }
  28.  
  29. bool union_set(ll a, ll b ) {
  30. a = find_set(a) ;
  31. b = find_set(b) ;
  32. if(a == b ) return false ;
  33. if(lab[a] > lab[b] ) swap(a, b ) ;
  34. history.push_back({a, lab[a] }) ;
  35. history.push_back({b, lab[b] }) ;
  36. lab[a] += lab[b] ;
  37. lab[b] = a ;
  38. return true ;
  39. }
  40.  
  41. void rollBack(ll te ) {
  42. while(history.size() > te ) {
  43. lab[history.back().fi ] = history.back().se ;
  44. history.pop_back() ;
  45. }
  46. }
  47.  
  48. bool cmp(ll x, ll y ) {
  49. return a[x] < a[y] ;
  50. }
  51.  
  52. void init() {
  53. history.clear() ;
  54. cin >> n >> m ;
  55. FOR(i, 1, n ) {
  56. cin >> a[i] ;
  57. lab[i] = - 1 ;
  58. }
  59. FOR(i, 1, m ) {
  60. ll x, y ;
  61. cin >> x >> y ;
  62. adj[x].push_back(y) ;
  63. adj[y].push_back(x) ;
  64. if(a[x] == a[y]) union_set(x, y ) ;
  65. }
  66. }
  67.  
  68. void solve() {
  69. ll ans = 0 ;
  70.  
  71. FOR(u, 1, n ) ans = max(ans, -lab[u]) ;
  72.  
  73. // FOR(i , 1 , n ) debug(lab[i]) ;
  74.  
  75. FOR(u, 1, n ) {
  76. sort(adj[u].begin(), adj[u].end(), cmp ) ;
  77. // cout << u << " u : " ;
  78. // for(ll v : adj[u]) cout << v << " " ; cout << el ;
  79. }
  80.  
  81.  
  82. FOR(u, 1, n ) {
  83. ll last = -1 ;
  84. ll snap = history.size() ;
  85. ll cur_color = - 1 ;
  86. for(ll v : adj[u] ) {
  87. if(last != a[v] ) {
  88. rollBack(snap ) ;
  89. last = a[v] ;
  90. cur_color = v ;
  91. }
  92. last = a[v] ;
  93. union_set(v, cur_color ) ;
  94. if(a[u] == a[v ]) ans = max(ans, -lab[find_set(cur_color)] ) ;
  95. else ans = max(ans, -lab[find_set(cur_color)] + 1 ) ;
  96. }
  97. rollBack(snap ) ;
  98. }
  99. FOR(i , 1 , n ) adj[i].clear() ;
  100. cout << ans << el ;
  101. }
  102.  
  103. _ROOT_ {
  104. // freopen(NAME".inp" , "r" , stdin);
  105. // freopen(NAME".out" , "w", stdout) ;
  106. ios_base::sync_with_stdio(0);
  107. cin.tie(0);
  108. cout.tie(0);
  109. int t = 1;
  110. cin >> t ;
  111. while(t--) {
  112. init();
  113. solve();
  114. }
  115. return (0&0);
  116. }
  117.  
Success #stdin #stdout 0.01s 29036KB
stdin
Standard input is empty
stdout
0