This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
inline char readchar() {
const int S = 1<<20; // buffer size
static char buf[S], *p = buf, *q = buf;
if(p == q && (q = (p=buf)+fread(buf,1,S,stdin)) == buf) return EOF;
return *p++;
}
inline int nextint() {
int x = 0, c = readchar(), neg = false;
while(('0' > c || c > '9') && c!='-' && c!=EOF) c = readchar();
if(c == '-') neg = true, c = readchar();
while('0' <= c && c <= '9') x = x*10 + (c^'0'), c = readchar();
if(neg) x = -x;
return x; // returns 0 if EOF
}
int main() {_
int n,m;
n=nextint();
m=nextint();
vector<vector<bool>> adj(n,vector<bool>(n));
vector<vector<bool>> s1(n,vector<bool>(n));
vector<vector<bool>> s2(n,vector<bool>(n));
int ans=0;
for(int i=0;i<m;i++){
int a,b;
a=nextint();
b=nextint();
a--;
b--;
if(adj[a][b]){
cout<<ans<<'\n';
continue;
}
queue<pair<int,int>> q;
q.push({a,b});
adj[a][b]=true;
ans++;
while(!q.empty() and (double)(clock())/CLOCKS_PER_SEC<4.9){
auto tmp=q.front();
q.pop();
int v=tmp.f;
int u=tmp.s;
//cout<<v+1<<' '<<u+1<<'\n';
if(!s1[v][u]){
for(int i=0;i<n;i++){
if(i!=v and i!=u and adj[u][i] and adj[i][u]){
if(!adj[v][i]){
adj[v][i]=true;
ans++;
if(adj[i][v]) q.push({v,i});
}
}
}
s1[v][u]=true;
}
if(adj[v][u] and adj[u][v] and !s2[v][u]){
for(int i=0;i<n;i++){
if(i!=v and i!=u and adj[i][v]^adj[i][u]){
if(adj[i][v]){
adj[i][u]=true;
ans++;
q.push({i,u});
}
else{
adj[i][v]=true;
ans++;
q.push({i,v});
}
}
}
s2[v][u]=s2[u][v]=true;
}
}
cout<<ans<<'\n';
}
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
joitter2.cpp: In function 'void setIO(std::string)':
joitter2.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |