// g++ 1.cpp -std=c++17 -O2 -I . #include using namespace std; #include using namespace atcoder; using ll = long long; using ld = long double; using vi = vector; using vvi = vector; using vll = vector; using vvll = vector; using vld = vector; using vvld = vector; using vst = vector; using vvst = vector; #define fi first #define se second #define pb push_back #define eb emplace_back #define pq_big(T) priority_queue,less> #define pq_small(T) priority_queue,greater> #define all(a) a.begin(),a.end() #define rep(i,start,end) for(ll i=start;i<(ll)(end);i++) #define per(i,start,end) for(ll i=start;i>=(ll)(end);i--) #define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end()) #include #include struct union_find_group{ private: int n; std::vector parent_size; // 正 親 // 負 -1*サイズ std::vector> group_info; // group の集合情報を常に管理 public: union_find_group(int sz){ n=sz; parent_size.resize(n,-1); group_info.resize(n); for(int i=0;i=|rj|の部分集合としたい if(parent_size[ri]>parent_size[rj]){ std::swap(ri,rj); } parent_size[ri]+=parent_size[rj]; parent_size[rj]=ri; for(int vartex_rj:group_info[rj]){ group_info[ri].emplace_back(vartex_rj); } return ri; } std::vector group(int i){ int ri=root(i); return group_info[ri]; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m;cin>>n>>m; union_find_group uf(n); vi in(n,0); vi out(n,0); rep(i,0,m){ int u,v;cin>>u>>v; u--;v--; in[v]++; out[u]++; uf.merge(u,v); } vvi v; rep(i,0,n){ if(uf.root(i)==i){ v.emplace_back(uf.group(i)); } } ll ans=v.size()-1; for(vi g:v){ vi d; for(int id:g){ d.emplace_back(in[id]-out[id]); cout<2)ans+=d2/2-1; if(g.size()==1&&d2==0)ans--; } cout<