提交时间:2025-02-10 21:20:35
运行 ID: 36327
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m, as, rudu[N]; bool book[N], ans[N], mem[N], mm[N]; vector<int> mp, g[N]; queue<int> q; int main() { int t; scanf("%d", &t); while(t--) { as = 0; memset(book, 0, sizeof(book)); memset(mem, 0, sizeof(mem)); memset(ans, 0, sizeof(ans)); memset(rudu, 0, sizeof(rudu)); memset(mm, 0, sizeof(mm)); scanf("%d%d", &n, &m); for(int i = n; i; --i) g[i].clear(); while(m--) { int x, c, y, d; scanf("%d%d%d%d", &x, &c, &y, &d); book[x] = book[y] = 1; if(c == 2 && d == 2) mp.push_back(x), mp.push_back(y); else if(c == 2) g[y].push_back(x), ++rudu[x]; else if(d == 2) g[x].push_back(y), ++rudu[y]; } for(int i = mp.size() - 1; ~i; --i) { int a = mp[i]; mp.pop_back(); ans[a] = mem[a] = 1; } for(int i = n; i; --i) if(!rudu[i] || mem[i]) q.push(i); while(!q.empty()) { int a = q.front(); if(!mem[a]) mem[a] = 1; mm[a] = 1; q.pop(); for(int i = g[a].size() - 1; ~i; --i) { int to = g[a][i]; if(!mem[to]) { mem[to] = ans[to] = 1; if(!mm[to]) q.push(to); } } } for(int i = n; i; --i) as += ans[i] + book[i]; printf("%d\n", as); } return 0; }