void flip(vvi& g, vvi& revg) { for(int i=0;i<g.size();i++) { for(int j=0;j<g[i].size();j++) { revg[g[i][j]].push_back(i); } } }
void showgraph(vvi& g) { for(int i=0;i<g.size();i++) { cout<<(i+1)<<" : "; for(int j=0;j<g[i].size();j++) cout<<g[i][j]+1<<" "; cout<<endl; } }
int solve(){ int maxLen = 0; for( int i = 0; i < map.size(); i++ ){ for( int j = 0; j < map[i].size(); j++ ){ //printf("%3d", dp(i, j)); if( maxLen < dp(i, j) ) maxLen = dp(i, j); } } return maxLen + 1; }
void solve() { for (ui i = 0; i < trees.size(); i++) { float min = -1; for (ui j = 0; j < trees.size(); j++) { if (i == j) continue; float dist = calcDist(trees[i][0], trees[i][1], trees[i][2], trees[j][0], trees[j][1], trees[j][2]); if (dist < min || min == -1) min = dist; } if (min < 10) answer[(int) min]++; } }
int lcp(int x, int y) { int ret = 0; if (x == y) return n - x; for (int k = P.size() - 1; k >= 0 && x < n && y < n; --k) if (P[k][x] == P[k][y]) { x += 1 << k; y += 1 << k; ret += 1 << k; } return ret; }
vvi find_sccs(const vvi& adj_list) { // Reset num_scc = ticks = 0; s = stack<int>(); d = vi(adj_list.size(), UNEXPLORED); low = vi(adj_list.size()); scc = vi(adj_list.size(), NO_SCC); in_stack = vector<bool>(adj_list.size(), false); // DFS all nodes for (int a = 0; a < adj_list.size(); ++a) { if (d[a] == UNEXPLORED) tarjan(adj_list, a); } // Count which scc every node belong to vvi sccs(num_scc); for (int i = 0; i < scc.size(); ++i) { sccs[scc[i]].push_back(i); } return sccs; }
void print (const vvi& s) { int N = s.size(); for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { printf ("%c", s[i][j]); } printf ("\n"); } printf("\n"); }
void flood_fill(vvi& g) { int ct = 0; memset(&visit,0,sizeof(visit)); for(int i=0;i<g.size();i++) { if(!visit[i]) { dfs(g,i); ct++; } } cout<<ct<<endl; }
Matrix operator+(const Matrix& mb) { vvi b = mb.a; int n = a.size(); int m = a[0].size(); Matrix tot; vvi c(n, std::vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { c[i][j] = a[i][j] + b[i][j]; } } tot.a = c; return tot; }
vvi initialize(const vvi& P, int m) { vvi T; T.resize(m); for ( auto i = 0; i < P.size(); ++i ) { if ( P[i].empty() ) continue; const auto& L = P[i]; for ( auto j = 0; j < L.size(); ++j ) T[ L[j] ].push_back(i); } return T; }
// calculate the factorial numbers // general idea: // https://blog.codechef.com/2009/07/02/tutorial-for-small-factorials/ // optimization: store all calculated numbers, calculate only when needed void fac(vvi &f, size_t n) { for (size_t highest = f.size(); highest < n; ++highest) { vi current; const int prev = highest-1; int carry = 0; for (vi::iterator it = f[prev].begin(); it != f[prev].end(); ++it) { int next = *it * (highest+1) + carry; carry = next / RANGE; current.push_back(next % RANGE); } while (carry) { current.push_back(carry % RANGE); carry = carry / RANGE; } f.push_back(current); } }
vvpi initialize(const vvi& P, int m) { vvpi T; T.resize(m); for ( auto i = 0; i < P.size(); ++i ) { if ( P[i].empty() ) continue; auto L = P[i]; sort(L.begin(), L.end()); for ( auto j = 0; j < L.size()-1; ++j ) T[ L[j] ].push_back(make_pair(i, L[j+1])); T[ L.back() ].push_back(make_pair(i, -1)); } return T; }
int componenets(vvi& arr){ int r = arr.size(); int c = arr[0].size(); vvi visited(r,vi(c,0)); int curcount = 0; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ //cout<<i<<" "<<j<< " "<<arr[i][j]<<" "<<visited[i][j]<<endl; if(arr[i][j]==1 && visited[i][j]==0){ //cout<<i<<" "<<j<<"called"<<endl; curcount++; dfs(arr,i,j,r,c,visited); } } } return curcount; }
int det(vvi &mat){ int n = mat.size(); int ret = 0, m1, m2; int i, j; for(int b = 0; b < n; b++){ m1 = 1, m2 = 1; for(int k = 0; k < n; k++){ i = k; j = (b+k)%n; m1 *= mat[i][j]; j = (n-(b+k+1)%n)%n; m2 *= mat[i][j]; } ret += m1 - m2; } return ret; }
int backtracking(vvi &table, int i, int j, int count) { int n = table.size(); int maximum = count; for (int ii = 0; ii < n; ii++) { for (int jj = 0; jj < n; jj++) { if (is_possible(table, ii, jj)) { table[ii][jj] = ROOK; int x = backtracking(table, ii, jj, count + 1); maximum = x > maximum ? x : maximum; table[ii][jj] = FREE; } } } return maximum; }
int main() { ios::sync_with_stdio(0); int tc; cin >> tc; while(tc--) { scc.clear(); int n, m; cin >> n >> m; AdjList.assign(n, vii()); for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; if(a != b) AdjList[a].push_back(ii(b, 0)); } int V = n; dfs_num.assign(V, 0); dfs_low.assign(V, 0); visited.assign(V, 0); node_to_scc_num.assign(V, -1); dfsNumberCounter = numSCC = scc_num = 0; for (int i = 0; i < V; i++) if (dfs_num[i] == 0) tarjanSCC(i); graph.assign(scc_num, vi()); for(int i = 0; i < scc.size(); i++) { for(int j = 0; j < scc[i].size(); j++) for(auto &e: AdjList[scc[i][j]]) if(node_to_scc_num[e.first] != i) graph[i].push_back(node_to_scc_num[e.first]); } memo.assign(scc_num, -1); int ans = 0; for(int i = 0; i < scc_num; i++) { int cur = scc[i].size(); ans = max(ans, solve(i) + cur); } cout << ans << endl; } return 0; }
int edmondKarps(vvi &adjList) { int mf = 0; while (true) { f = 0; bitset<MAX_V> vis; vis[s] = true; queue<int> q; q.push(s); vi p(adjList.size(), -1); while (!q.empty()) { int u = q.front(); q.pop(); if (u == t) break; for (int j = 0; j < adjList[u].size(); j++) { int v = adjList[u][j]; if (res[u][v] > 0 && !vis[v]) vis[v] = true, q.push(v), p[v] = u; } } augment(t, INT_MAX, p); if (f == 0) break; mf += f; } return mf; }
int bfs(vvi &graph, int from, int to) { vi distance(graph.size(), UND); distance[from] = 0; queue<int> q; q.push(from); while (not q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < graph[u].size(); i++) { int v = graph[u][i]; if (distance[v] == UND) { distance[v] = distance[u] + 1; q.push(v); } } } return distance[to]; }
void printVVI(vvi tmp) { for(int i = 0; i < tmp.size(); ++i) printVI(tmp[i]); cout << endl; }
bool isValid(int i, int j){ return (0 <= i && i < map.size() && 0 <= j && j < map[i].size()); }
void print (vvi s) { for (int i=0; i<s.size(); i++) print (s[i], to_string(i)+" : "); printf("\n----\n"); }
void change(vvi &v, int a, int b) { for (int i = 0; i < v.size(); i++) { swap(v[i][a], v[i][b]); } }